Take the following loop:
for(var i=0; i<100; ++i){ let result = await some_slow_async_function(); do_something_with_result(); }
Does await
block the loop? Or does the i
continue to be incremented while await
ing?
Is the order of do_something_with_result()
guaranteed sequential with regard to i
? Or does it depend on how fast the await
ed function is for each i
?
You should make sure you never block the Event Loop. In other words, each of your JavaScript callbacks should complete quickly. This of course also applies to your await 's, your Promise.
Though it creates a confusion, in reality async and await will not block the JavaScript main thread. Like mentioned above they are just syntactic sugars for promise chaining.
await only blocks the execution of the next lines of code in an async function and doesn't affect the promise execution.
The await operator doesn't block the thread that evaluates the async method. When the await operator suspends the enclosing async method, the control returns to the caller of the method.
- Does
await
block the loop? Or does thei
continue to be incremented whileawait
ing?
"Block" is not the right word, but yes, i does not continue to be incremented while awaiting. Instead the execution jumps back to where the async
function was called, providing a promise as return value, continuing the rest of the code that follows after the function call, until the code stack has been emptied. Then when the awaiting is over, the state of the function is restored, and execution continues within that function. Whenever that function returns (completes), the corresponding promise -- that was returned earlier on -- is resolved.
- Is the order of
do_something_with_result()
guaranteed sequential with regard toi
? Or does it depend on how fast theawait
ed function is for eachi
?
The order is guaranteed. The code following the await
is also guaranteed to execute only after the call stack has been emptied, i.e. at least on or after the next microtask can execute.
See how the output is in this snippet. Note especially where it says "after calling test":
async function test() { for (let i = 0; i < 2; i++) { console.log('Before await for ', i); let result = await Promise.resolve(i); console.log('After await. Value is ', result); } } test().then(_ => console.log('After test() resolved')); console.log('After calling test');
As @realbart says, it does block the loop, which then will make the calls sequential.
If you want to trigger a ton of awaitable operations and then handle them all together, you could do something like this:
const promisesToAwait = []; for (let i = 0; i < 100; i++) { promisesToAwait.push(fetchDataForId(i)); } const responses = await Promise.all(promisesToAwait);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With