Skip to content

Commit

Permalink
Add guide from issue #206 (#228)
Browse files Browse the repository at this point in the history
Add guide from issue #206

Closes #206

Co-authored-by:  Matthew Phillips <[email protected]>
  • Loading branch information
ehuelsmann and matthewp authored Dec 14, 2024
1 parent cd53d0f commit 87d7216
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/guides.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ These guides are meant to help you along your journey learning Finite State Mach

* __[Composition](./guides/composition.html)__: How to use functional composition when building state machines, to maximize code reuse where possible.
* __[Nested States](./guides/nested-states.html)__: Create machines with nested states by [invoking](./api/invoke.html) other machines.
* __[Comparison with XState](./guides/comparison-with-xstate.html)__: Differences and tradeoffs between Robot and [XState](https://xstate.js.org).
* __[Awaiting asynchronous completion](./guides/awaiting-asynchronous-completion.html)__: How to await a machine to enter a `final` state.
* __[Comparison with XState](./guides/comparison-with-xstate.html)__: Differences and tradeoffs between Robot and [XState](https://xstate.js.org).
30 changes: 30 additions & 0 deletions docs/guides/awaiting-asynchronous-execution.md
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.

0 comments on commit 87d7216

Please sign in to comment.