Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to start and await for a state machine's asynchronous execution? #206

Open
mpenna opened this issue Nov 7, 2023 · 2 comments
Open

Comments

@mpenna
Copy link

mpenna commented Nov 7, 2023

I have this scenario where the state machine will be frequently triggered/started by an external process (e.g.: a cron-like task manager), which will need to block until whatever asynchronous processing the state machine has been configured to execute finishes. Is this achievable? State would be serialized/persisted and pulled/hydrated back upon every triggered execution. However, I'm not sure how the blocking of the external (controller) process would be handled.

Thanks.

@mpenna mpenna changed the title How to start and await for a state machine asynchronous execution? How to start and await for a state machine's asynchronous execution? Nov 7, 2023
@matthewp
Copy link
Owner

matthewp commented Nov 7, 2023

Your finish is "finished" when it hits a final state, a state which has no transitions. What I would probably do is something like this:

let resolve;
let promise = new Promise(_resolve => {
  resolve = _resolve;
})

service = interpret(machine, () => {
  if(machine.state.value.final) {
    resolve();
  }
});

await promise;
// All done!

@mpenna
Copy link
Author

mpenna commented Nov 7, 2023

Nice! Let me try that strategy out and see how it goes.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants