Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misleading information around await's interaction with microtask/task queues #24177

Open
senocular opened this issue Feb 5, 2023 · 1 comment
Labels
Content:JS JavaScript docs help wanted If you know something about this topic, we would love your help!

Comments

@senocular
Copy link
Contributor

senocular commented Feb 5, 2023

MDN URL

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await

What specific section or headline is this issue about?

Control flow effects of await

What information was incorrect, unhelpful, or incomplete?

...all code that depends on the expression's value is paused and pushed into the microtask queue.

await does not inherently push anything into the microtask queue, at least not unless the promise being awaited is already settled. The microtask queue is where promise tasks go after the promise is settled. await simply pauses the function. The microtask of resuming the function is added to the queue when the promise ultimately settles.

The "microtask queue" link to the EventLoop page also has no mention of the microtask queue.

The main thread is then freed for the next task in the event loop. This happens even if the awaited value is an already-resolved promise or not a promise.

This only happens if the promise is not already settled (or does not get settled within further execution of the current task). If settled, function continuation is added to the microtask queue and the current task of the event loop continues to run until its microtask queue is empty. You can create an event loop-blocking async function demonstrating this

// blocks the event loop
async function loop(){
  await Promise.resolve()
  loop()
}
loop()

Despite the use of await, the main thread is not freed to run the next task.

What did you expect to see?

A more accurate description await and its interactions with the task and microtask queues.

The microtask link is probably better directed to:
https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide
(see comment below)

Do you have any supporting links, references, or citations?

https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide

Do you have anything more you want to share?

No response

MDN metadata

Page report details
@senocular senocular added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Feb 5, 2023
@github-actions github-actions bot added Content:JS JavaScript docs Content:WebAPI Web API docs labels Feb 5, 2023
@Josh-Cena Josh-Cena added help wanted If you know something about this topic, we would love your help! and removed Content:WebAPI Web API docs needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Feb 5, 2023
@Josh-Cena
Copy link
Member

I agree there's something subtle about the phrasing here, but I won't be able to get to this soon. If you have solid ideas, feel free to send a PR.

The "microtask queue" link to the EventLoop page also has no mention of the microtask queue.

That's an unfortunate miss. I planned to add microtasks to the event loop page, but never got to it, which is why the link became dangling. I would prefer to keep it as-is, though, because this should be a core topic, not particularly an HTML topic (there's only one task queue in the language).

This only happens if the promise is not already settled (or does not get settled within further execution of the current task). If settled, function continuation is added to the microtask queue and the current task of the event loop continues to run until its microtask queue is empty. You can create an event loop-blocking async function demonstrating this

// blocks the event loop
async function loop(){
  await Promise.resolve()
  loop()
}
loop()

Despite the use of await, the main thread is not freed to run the next task.

Nice construction. I would love to see it on the page!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content:JS JavaScript docs help wanted If you know something about this topic, we would love your help!
2 participants