question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Async actions don't return a proper promise

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
christianalfonicommented, Feb 7, 2019

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 😃

0reactions
luisherranzcommented, Feb 13, 2019

Working perfectly now in @next: https://codesandbox.io/s/r073jp1jl4

Both actions and onInitialize 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found