Skip to content

Proposal: self.queueMicrotask(f) #512

Closed

Description

@domenic

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.

/ccing some implementers: @hober @travisleithead @bzbarsky

Activity

  1. added
    addition/proposal
    on Jan 13, 2016
  2. bzbarsky commented on Jan 13, 2016

    @bzbarsky
    Contributor

    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....

  3. bzbarsky commented on Jan 13, 2016

    @bzbarsky
    Contributor

    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.

  4. domenic commented on Jan 13, 2016

    @domenic
    MemberAuthor

    That'd be lovely if you could; thank you.

  5. ianb commented on Jan 13, 2016

    @ianb

    What is the difference between window.enqueueMicrotask(f) and window.setTimeout(f) ?

  6. khuey commented on Jan 13, 2016

    @khuey

    setTimeout returns to the event loop and processes events.

  7. domenic commented on Jan 13, 2016

    @domenic
    MemberAuthor

    For more information on that question I'd suggest reading the spec which has detailed sections on setTimeout and on enqueuing a microtask.

  8. smaug---- commented on Jan 14, 2016

    @smaug----

    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.

  9. domenic commented on Jan 14, 2016

    @domenic
    MemberAuthor
  10. smaug---- commented on Jan 14, 2016

    @smaug----

    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.

  11. domenic commented on Jan 14, 2016

    @domenic
    MemberAuthor

    Well, whatever you want to do, I imagine it will fall under one of

    the user agent may either throw a QuotaExceededError exception, abort the script without an exception, prompt the user, or throttle script execution.

    so I still think it's covered by the existing spec.

  12. smaug---- commented on Jan 14, 2016

    @smaug----

    Except that if we add some limitation, it needs to work consistently across browsers.

  13. domenic commented on Jan 14, 2016

    @domenic
    MemberAuthor

    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.

  14. smaug---- commented on Jan 14, 2016

    @smaug----

    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.

  15. 26 remaining items

  16. saschanaz commented on Apr 30, 2017

    @saschanaz
    Member

    I think there was a similar try with setImmediate, is there any major reason to make a new spec?

  17. jakearchibald commented on Apr 30, 2017

    @jakearchibald
    Contributor

    setImmediate queues a task, not a microtask.

  18. added a commit that references this issue on Jun 23, 2017
    1e48bc3
  19. changed the title [-]Proposal: window.enqueueMicrotask(f)[/-] [+]Proposal: self.queueMicrotask(f)[/+] on Jul 9, 2018
  20. domenic commented on Jul 9, 2018

    @domenic
    MemberAuthor

    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.

  21. added a commit that references this issue on Jul 9, 2018
    e15f47c
  22. travisleithead commented on Jul 9, 2018

    @travisleithead
    Member

    @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.

  23. domenic commented on Jul 10, 2018

    @domenic
    MemberAuthor

    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?

  24. dstorey commented on Jul 16, 2018

    @dstorey
    Member

    Yes

  25. added a commit that references this issue on Jul 23, 2018
    f6f9f9e
  26. added a commit that references this issue on Aug 1, 2018
    9d7cf12
  27. added a commit that references this issue on Jan 8, 2019
    a9a3629
  28. added 2 commits that reference this issue on Feb 15, 2019
    dc9caf3
    2621705
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    addition/proposal

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions