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

Async/Await. How to use in Actions? #34

Open
mathenshall opened this issue May 30, 2018 · 2 comments
Open

Async/Await. How to use in Actions? #34

mathenshall opened this issue May 30, 2018 · 2 comments

Comments

@mathenshall
Copy link

Hi David,

I looked through the issues, and you have spoken to the desire to keep the machine 'synchronous'. Which makes a lot of sense.

However, I have some actions that go off and do some web requests, which return a Promise. The ideal way of managing this is to use Await, as it keeps the machine synchronous to itself. However, my understanding is that I need to make all the calling functions from state.evaluate() to the action set with '.entry' or '.effect' 'async', and in several places in between explicitly call 'await xxxx()'.

Am I missing something? Or do I need to fork your library and add a state.evaluateAsync() et al methods?

@mesmo
Copy link
Member

mesmo commented Jun 3, 2018

My initial inclination would be to have a subsection of your state machine represent the web request - a state called pendingResponse or similar, that on entry initiates the call, then in your .then callback, fires an event back into the machine causing it to transition out of pendingResponse.

@mathenshall
Copy link
Author

Thanks for the input. I initially tried the approach you describe. There is a point in your code where you iterate through the all the actions on a transition. I believe the semantics are such that each action should complete before the next one is 'fired' (ie the exit action from the from state before the transition action before the entry of the to state etc.). I could of redesigned the machine however it was getting really complex and I found by allowing an action to return a promise and then machine awaiting for it to be fulfilled really simplified the machine in this instance. It also allowed for the 'evaluate' call to return a promise that could be awaited, that greatly simplified the calling code.

I ended up forking and created a version that provides async/await semantics but I wonder if it would be possible to do some sort of clever stuff with generics to allow for either option to be chosen.

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