DEV Community

Discussion on: ⭐️🎀 JavaScript Visualized: Promises & Async/Await

Collapse
 
silverbullet069 profile image
VH • Edited

Now I understand "The await keyword suspends the async function, whereas the Promise body would've kept on being executed if we would've used then!"

If you call:

  • a function that return a Promise
  • an anonymous Promise that was defined inside that function.

It will run the callback inside that Promise and resolved that Promise instantly before calling then() since Promise constructor called its callback function.

Okay, now, inside an async function, when you encounters an await statement, you run the awaited command (either a function that return a Promise const res = await thisFuncReturnAPromise(), or that Promise directly const res = await promise), after that, you put that async function inside microstack, waiting to be resumed. That means the async function has been suspended.

P/s: in async function, we all know that async function will return a Promise, but we usually allude this because the return value of an async function is only valuable when that Promise call then() or being passed after await keyword in another async function.

Collapse
 
iosonoagenda profile image
Ettore Ongaro

Maybe if you share a piece of code can be more verbose man