Async actions don't return a proper promise
See original GitHub issueI’m wondering why async actions don’t return a proper promise:
actions: {
waitFor3sec: async () => {
await new Promise(resolve => setTimeout(resolve, 3000));
return "done";
}
}
// ...
console.log("now");
await overmind.actions.waitFor3sec(); // <- this seems to be sync
console.log("after 3 seconds");
Complete example here: https://codesandbox.io/s/6loqylpk3k
They also don’t seem to return the returned value ('done'
in this case).
We are currently using MobxStateTree but thinking about moving to Overmind.
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (9 by maintainers)
Top Results From Across the Web
Use Promise.all to Stop Async/Await from Blocking Execution ...
Promises are extremely powerful for handling asynchronous operations in JavaScript. ... Promises don't return values for use outside themselves.
Read more >Why do I need to await an async function when it is not ...
All async functions return a promise. All of them. That promise will eventually resolve with whatever value you return from the async ......
Read more >How to use promises - Learn web development | MDN
A promise is an object returned by an asynchronous function, which represents the current state of the operation. At the time the promise...
Read more >Understand promises before you start using async/await
First. we dispatch `getFoo` and `getBar` and save the promises they return in `fooPromise` and `barPromise`. · These actions are now in progress, ......
Read more >Make your async functions fast with Promise.all - Rajat Explains
𝟷. Promises don't return values for use outside themselves. There's no straightforward way to get the data out of a Promise; you have...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hi guys! 😃
That the returned promise form an action is not “awaitable” must be a bug. Thanks for the suggested fix @JM-Mendez! I will look into that and make it fit nicely in 😃
The reason actions does not return a value is really to avoid patterns where you decouple your logic into multiple actions. When you get into the need of composing logic we encourage you to look at the functional API. That will allow you to run actions in order, map in new values etc. https://www.overmindjs.org/guides/intermediate/04_goingfunctional?view=react&typescript=false
An additional benefit here is that Overmind will also understand what is “coherent” logic, allowing you to debug it better, as seen in this devtools video: https://www.overmindjs.org/videos/devtools-introduction?view=react&typescript=false
But guys, this is awesome, thanks so much for discussing and sharing. Stay tuned for bug fix 😃
Working perfectly now in
@next
: https://codesandbox.io/s/r073jp1jl4Both actions and onInitialize 😃