Home > Enterprise >  Do await functions have to resolve to Promises for them to run sequentailly?
Do await functions have to resolve to Promises for them to run sequentailly?

Time:04-06

I was wondering if functions called with the await keyword only run in order if they resolve to promises. If so, how does the interpreter know that these functions are going to resolve to promises and to not run them concurrently?

CodePudding user response:

You can only (validly) await function calls that are explicitly marked async. When the interpreter looks at the function itself and sees it's got the async keyword, it knows it will always return a Promise. From the MDN page regarding async functions:

Async functions always return a promise. If the return value of an async function is not explicitly a promise, it will be implicitly wrapped in a promise.

CodePudding user response:

  1. I was wondering if functions called with the await keyword only run in order if they resolve to promises.: Does awaiting a non-Promise have any detectable effect?
  2. If so, how does the interpreter know that these functions are going to resolve to promises and to not run them concurrently? Async processes can absolutely run concurrently. If you are making a net or FS request, something else can run in JavaScript while the data is being marshalled
  • Related