Skip to content

Commit 6c8ef77

Browse files
committed
Tweak listener middleware example
1 parent 1e85331 commit 6c8ef77

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

docs/api/createListenerMiddleware.mdx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,21 @@ listenerMiddleware.startListening({
5454
// Use the listener API methods to dispatch, get state,
5555
// unsubscribe the listener, start child tasks, and more
5656
listenerApi.dispatch(todoAdded('Buy pet food'))
57-
listenerApi.unsubscribe()
57+
58+
// Spawn "child tasks" that can do more work and return results
59+
const task = listenerApi.fork(async (forkApi) => {
60+
// Can pause execution
61+
await forkApi.delay(5)
62+
// Complete the child by returning a value
63+
return 42
64+
})
65+
66+
const result = await task.result
67+
// Unwrap the child result in the listener
68+
if (result.status === 'ok') {
69+
// Logs the `42` result value that was returned
70+
console.log('Child succeeded: ', result.value)
71+
}
5872
}
5973
},
6074
})

0 commit comments

Comments
 (0)