-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add guide from issue #206 Closes #206 Co-authored-by: Matthew Phillips <[email protected]>
- Loading branch information
1 parent
cd53d0f
commit 87d7216
Showing
2 changed files
with
32 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
layout: page.njk | ||
title: Awaiting asynchronous execution | ||
tags: guide | ||
permalink: guides/awaiting-asynchronous-execution.md | ||
--- | ||
|
||
In a scenario where it's necessary to `await` the machine to enter a `final` | ||
state (a state which has no transitions), the `onChange` callback (the second argument to `interpret`) can be used | ||
to resolve a promise. The promise can be externally awaited. | ||
|
||
```js | ||
let resolve; | ||
let promise = new Promise(_resolve => { | ||
resolve = _resolve; | ||
}) | ||
|
||
service = interpret(machine, () => { | ||
if(machine.state.value.final) { | ||
resolve(); | ||
} | ||
}); | ||
|
||
await promise; | ||
// All done! | ||
``` | ||
|
||
This is particularly useful for state machines which consist entirely | ||
of `immediate` and `invoke` states: these model execution flows which do | ||
not depend on external events for their execution and state transition. |