filmov
tv
Understanding the Running Time of async/await and Promise.all in JavaScript

Показать описание
---
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
The Problem: Misunderstanding Promises' Behavior
Consider the following scenario: you create three promises, each set to resolve after 2 seconds. You might expect that waiting for each promise sequentially with await would take a total of 6 seconds (2 seconds for each). However, the total execution time turns out to be only 2 seconds. How is this possible?
Here’s the code example to illustrate this confusion:
[[See Video to Reveal this Text or Code Snippet]]
Console Output
You might expect the console to output a total time of 6 seconds, but instead, you see something like:
[[See Video to Reveal this Text or Code Snippet]]
The Solution: Understanding Promises
1. Immediate Construction
All three promises are created immediately: 3 promises exist in parallel time, each set to resolve after 2 seconds.
[[See Video to Reveal this Text or Code Snippet]]
All the promises are awaited simultaneously. Since they are already resolving in the background, they all finish after 2 seconds, and you get the results instantly.
3. Using async/await
In the case of async/await:
[[See Video to Reveal this Text or Code Snippet]]
The first await statement waits for PromiseOne to resolve, which takes 2 seconds.
Meanwhile, PromiseTwo and PromiseThree are already resolved by the time we reach them. So, calling await on them incurs no additional waiting time.
Key Takeaway
The overall execution time for both methods ends up being the same because:
All promises are created and started at the same time.
Once the first promise resolves, the others do as well, leading to no extra delay for subsequent await statements.
Conclusion: Understanding Execution Patterns
It's important to recognize that if you constructed subsequent promises after waiting for the previous ones, you would indeed end up with serial execution—much the way you initially expected. For example:
[[See Video to Reveal this Text or Code Snippet]]
In this case, total time would be 6 seconds because you're waiting for each promise to be created and resolved one after the other.
Understanding these nuances allows developers to write more efficient asynchronous code, leveraging the power of promises effectively and avoiding common pitfalls.
Feel free to dive deeper into how promises work to enhance your JavaScript skills and optimize your applications.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
The Problem: Misunderstanding Promises' Behavior
Consider the following scenario: you create three promises, each set to resolve after 2 seconds. You might expect that waiting for each promise sequentially with await would take a total of 6 seconds (2 seconds for each). However, the total execution time turns out to be only 2 seconds. How is this possible?
Here’s the code example to illustrate this confusion:
[[See Video to Reveal this Text or Code Snippet]]
Console Output
You might expect the console to output a total time of 6 seconds, but instead, you see something like:
[[See Video to Reveal this Text or Code Snippet]]
The Solution: Understanding Promises
1. Immediate Construction
All three promises are created immediately: 3 promises exist in parallel time, each set to resolve after 2 seconds.
[[See Video to Reveal this Text or Code Snippet]]
All the promises are awaited simultaneously. Since they are already resolving in the background, they all finish after 2 seconds, and you get the results instantly.
3. Using async/await
In the case of async/await:
[[See Video to Reveal this Text or Code Snippet]]
The first await statement waits for PromiseOne to resolve, which takes 2 seconds.
Meanwhile, PromiseTwo and PromiseThree are already resolved by the time we reach them. So, calling await on them incurs no additional waiting time.
Key Takeaway
The overall execution time for both methods ends up being the same because:
All promises are created and started at the same time.
Once the first promise resolves, the others do as well, leading to no extra delay for subsequent await statements.
Conclusion: Understanding Execution Patterns
It's important to recognize that if you constructed subsequent promises after waiting for the previous ones, you would indeed end up with serial execution—much the way you initially expected. For example:
[[See Video to Reveal this Text or Code Snippet]]
In this case, total time would be 6 seconds because you're waiting for each promise to be created and resolved one after the other.
Understanding these nuances allows developers to write more efficient asynchronous code, leveraging the power of promises effectively and avoiding common pitfalls.
Feel free to dive deeper into how promises work to enhance your JavaScript skills and optimize your applications.