Issue with "Making asynchronous programming easier with async …": a bug-prone example in "The downsides of async/await"
MDN URL: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
What information was incorrect, unhelpful, or incomplete?
Hello, I think the example "fast-async-await.html" in the "The downsides of async/await" section maybe bug-prone.
// The code that I think maybe bug-prone
async function timeTest() {
const timeoutPromise1 = timeoutPromise(3000);
const timeoutPromise2 = timeoutPromise(3000);
const timeoutPromise3 = timeoutPromise(3000);
await timeoutPromise1;
await timeoutPromise2;
await timeoutPromise3;
}
As discussed at the end of "Description" section in async function, if I build my async function as "fast-async-await.html" with only difference that timeoutPromise2 will be settled before timeoutPromise1 and reject with an error, the error would keep unhandled until timeoutPromise1 become settled. Because timeoutPromise2 will not be "wired into" the promise chain until control returns from timeoutPromise1
Specific section or headline?
The "fast-async-await.html" example in "The downsides of async/await" section
What did you expect to see?
Did you test this? If so, how?
I tested the following code in the console of "Microsoft Edge Version 89.0.774.63 (Official build) (64-bit)"
async function foo() {
const p1 = new Promise((resolve) => setTimeout(() => resolve('1'), 10000))
const p2 = new Promise((_, reject) => setTimeout(() => reject('2'), 500))
const result1 = await p1;
const result2 = await p2;
}
foo().catch((e) => {console.error(e)});
I think you could see the rejection reason '2' thrown by p2 keeping uncaught until p1 becomes settled after 10 seconds.
MDN Content page report details
- Folder:
en-us/learn/javascript/asynchronous/async_await - MDN URL: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await
- GitHub URL: https://github.com/mdn/content/blob/main/files/en-us/learn/javascript/asynchronous/async_await/index.html
- Last commit: https://github.com/mdn/content/commit/79f50609cb6bcb66e36cc3f9c4fa0537870b1b9a
- Document last modified: 2021-03-25T14:03:43.000Z