You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In talking with the Angular team (mostly @mhevery), there is interest in a simple low-level method for enqueuing a microtask from author code. I've heard this from a number of authors in the past. It seems simple enough and easy to add to the spec, if we can get any other implementations besides Chrome interested.
Right now authors are doing this via a number of hacks, like creating a text node and a MutationObserver and twiddling the text node every time they want to trigger a microtask, or using Promise.resolve().then(f) (which creates a promise object and stores any thrown errors there, instead of letting them propagate to "report an exception").
There are many use cases, which I'm sure we can assemble in depth, but let me just start with the following reasons which will hopefully be enough to convince people:
From an academic point of view, this is a low-level primitive and there's no reason to lock it up behind higher-level concepts like promises and mutation observers.
From a practical point of view, libraries very often want to guarantee asynchrony (to avoid Zalgo), without taking the overhead of a full "macro" task like setTimeout(f, 0).
I'd like to first focus this thread on finding implementer support: if we spec this, will you implement it? After that, we can start bikeshedding the name; please don't do so beforehand.
This makes sense as a primitive to have. The only concern is what we do once it starts getting abused (see setTimeout throttling). Of course we'd have to figure that out for Promise and MutationObserver too....
I would also suggest posting to the Mozilla dev-platform mailing list to get a wider set of Mozilla people looking at this. I can do that for you if you want; please let me know.
Abusing is an issue, and yes, we may need to do something to that with MutationObserver and
Promise case too. (I can see long Promise chains to be rather horrible from responsiveness point of view.) Abuse is here even more an issue than with setTimeout, since microtask is kind of an async concept, but only from js execution side, not from event loop.
We should think about abusing issues before we add the API. But in general sounds like a good idea.
I'm not so sure.
We're giving a new model for running JS almost-async. That 'almost' can be hard to understand. (at least based on my experience - I've had to explain microtasks quite a few times).
Initially microtasks were for one particular case only - running MutationObserver callbacks, and in that case abusing is less likely. Promises make abusing way more likely, and raw microtask callbacks perhaps even more.
But it is not clear to me what we could do to prevent abuse.
Why? The slow script dialog doesn't work consistently across browsers today. Recursive microtasks are essentially the same as recursive functions: it allows an extra stack frame to unwind in between, but there's otherwise no difference. So I think they should be treated the same way.
ok, so you're worried about blocking UI totally with effectively while(true);
I'm also worried about moving to a model where people start to use more
window.enqueueMicrotask and less window.setTimeout..
Having several nested microtasks may easily end up pushing the next rAF callback time to be
too late. setTimeout has less that problem since it is truly async, and may just happen after rAF if needed.
Chrome is currently implementing the proposal as specced in #2789. Are any other vendors interested in shipping queueMicrotask()? Lots of discussion with Gecko folks above; what about Safari (@cdumez@hober) or Edge (@dstorey@travisleithead)?
Note how #2789 has web developer-facing text about when this is appropriate which should help at least a bit with some of the Gecko discussions here.
@dstorey, @arronei and I are glad to see the Author note. I'm a little concerned about developers easily falling into the trap of scheduling the next callback from within the callback (e.g., like rAF) that will lead to accidental infinite callbacks without a render/paint. But there's no solution I can think of to prevent that while still enabling the scenario :-). No objections here.
Thanks @travisleithead and folks! We could indeed add some examples drawing explicit equivalence between recursive queueMicrotask and recursive sync functions / sync while loops.
Activity
bzbarsky commentedon Jan 13, 2016
This makes sense as a primitive to have. The only concern is what we do once it starts getting abused (see setTimeout throttling). Of course we'd have to figure that out for Promise and MutationObserver too....
bzbarsky commentedon Jan 13, 2016
I would also suggest posting to the Mozilla dev-platform mailing list to get a wider set of Mozilla people looking at this. I can do that for you if you want; please let me know.
domenic commentedon Jan 13, 2016
That'd be lovely if you could; thank you.
bzbarsky commentedon Jan 13, 2016
https://groups.google.com/forum/#!topic/mozilla.dev.platform/NoKg9nX34vE or https://lists.mozilla.org/pipermail/dev-platform/2016-January/013056.html depending on how you prefer to read your mailing list threads.
ianb commentedon Jan 13, 2016
What is the difference between
window.enqueueMicrotask(f)andwindow.setTimeout(f)?khuey commentedon Jan 13, 2016
setTimeout returns to the event loop and processes events.
domenic commentedon Jan 13, 2016
For more information on that question I'd suggest reading the spec which has detailed sections on setTimeout and on enqueuing a microtask.
smaug---- commentedon Jan 14, 2016
Abusing is an issue, and yes, we may need to do something to that with MutationObserver and
Promise case too. (I can see long Promise chains to be rather horrible from responsiveness point of view.) Abuse is here even more an issue than with setTimeout, since microtask is kind of an async concept, but only from js execution side, not from event loop.
We should think about abusing issues before we add the API. But in general sounds like a good idea.
domenic commentedon Jan 14, 2016
I think abuse is taken care of by the slow script dialog clause already: https://html.spec.whatwg.org/#killing-scripts
smaug---- commentedon Jan 14, 2016
I'm not so sure.
We're giving a new model for running JS almost-async. That 'almost' can be hard to understand. (at least based on my experience - I've had to explain microtasks quite a few times).
Initially microtasks were for one particular case only - running MutationObserver callbacks, and in that case abusing is less likely. Promises make abusing way more likely, and raw microtask callbacks perhaps even more.
But it is not clear to me what we could do to prevent abuse.
domenic commentedon Jan 14, 2016
Well, whatever you want to do, I imagine it will fall under one of
so I still think it's covered by the existing spec.
smaug---- commentedon Jan 14, 2016
Except that if we add some limitation, it needs to work consistently across browsers.
domenic commentedon Jan 14, 2016
Why? The slow script dialog doesn't work consistently across browsers today. Recursive microtasks are essentially the same as recursive functions: it allows an extra stack frame to unwind in between, but there's otherwise no difference. So I think they should be treated the same way.
smaug---- commentedon Jan 14, 2016
ok, so you're worried about blocking UI totally with effectively while(true);
I'm also worried about moving to a model where people start to use more
window.enqueueMicrotask and less window.setTimeout..
Having several nested microtasks may easily end up pushing the next rAF callback time to be
too late. setTimeout has less that problem since it is truly async, and may just happen after rAF if needed.
26 remaining items
saschanaz commentedon Apr 30, 2017
I think there was a similar try with setImmediate, is there any major reason to make a new spec?
jakearchibald commentedon Apr 30, 2017
setImmediatequeues a task, not a microtask.Add self.queueMicrotask()
[-]Proposal: window.enqueueMicrotask(f)[/-][+]Proposal: self.queueMicrotask(f)[/+]domenic commentedon Jul 9, 2018
Resurrecting this old thread:
Chrome is currently implementing the proposal as specced in #2789. Are any other vendors interested in shipping queueMicrotask()? Lots of discussion with Gecko folks above; what about Safari (@cdumez @hober) or Edge (@dstorey @travisleithead)?
Note how #2789 has web developer-facing text about when this is appropriate which should help at least a bit with some of the Gecko discussions here.
Add self.queueMicrotask()
travisleithead commentedon Jul 9, 2018
@dstorey, @arronei and I are glad to see the Author note. I'm a little concerned about developers easily falling into the trap of scheduling the next callback from within the callback (e.g., like
rAF) that will lead to accidental infinite callbacks without a render/paint. But there's no solution I can think of to prevent that while still enabling the scenario :-). No objections here.domenic commentedon Jul 10, 2018
Thanks @travisleithead and folks! We could indeed add some examples drawing explicit equivalence between recursive queueMicrotask and recursive sync functions / sync while loops.
I'm a little unsure how we should interpret "No objections here" for the purposes of https://whatwg.org/working-mode#additions; would you support adding this to the specification?
dstorey commentedon Jul 16, 2018
Yes
Add self.queueMicrotask()
Add self.queueMicrotask()
Add self.queueMicrotask()
Add self.queueMicrotask()
Add self.queueMicrotask()