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

Enabling optional higher precedence for 'queueMicrotask' #4891

Closed
jfet97 opened this issue Sep 7, 2019 · 5 comments
Closed

Enabling optional higher precedence for 'queueMicrotask' #4891

jfet97 opened this issue Sep 7, 2019 · 5 comments

Comments

@jfet97
Copy link

jfet97 commented Sep 7, 2019

Enqueuing a callback using queueMicrotask: queueMicrotask(() => console.log(42))
or using Promise.resolve(): Promise.resolve().then(() => console.log(42))
currently has the same temporal outcome:

(function() {
   console.log(1)
   Promise.resolve().then(() => console.log(2))
   queueMicrotask(() => console.log(3))
})();
// 1 2 3
// not 1 3 2

It is the correct behaviour, but the usefulness of the new API begins to be less relevant. Clarity apart.

I propose an enhancement, that is the possibility to enqueue a microtask not at the bottom of the microtask queue but at the top of it.
It could be easily implemented using a boolean flag, defaulted to false:

(function() {
   console.log(1)
   Promise.resolve().then(() => console.log(2))
   queueMicrotask(() => console.log(3), true)
})();
// 1 3 2
// not 1 2 3
@Yay295
Copy link
Contributor

Yay295 commented Sep 8, 2019

Interesting idea, though I suspect the code of anyone who uses actually this will become quite a bit more confusing.

@jfet97
Copy link
Author

jfet97 commented Sep 8, 2019

Interesting idea, though I suspect the code of anyone who uses actually this will become quite a bit more confusing.

Considering the fact that the Microtask API is primarily designed for libraries implementers, people who know what they're doing, I think it's an acceptable solution.

@domenic
Copy link
Member

domenic commented Sep 8, 2019

This isn't possible, as the microtask queue is specifically a queue, so it only allows enqueuing items (at the end), not jumping the queue.

Closing, but happy to continue discussing how the microtask queue works in the closed thread.

@domenic domenic closed this as completed Sep 8, 2019
@jfet97
Copy link
Author

jfet97 commented Sep 9, 2019

@domenic I'd like to ask two things:

  1. Why we are forced to use a queue instead of a structure that would allow us to put elements both at the bottom and at the top, to increase priority on demand?
  2. Why should I prefer the Microtask API instead of Promise.resolve().then(cb), which has the same outcome but it is more widely supported?

@domenic
Copy link
Member

domenic commented Sep 9, 2019

For understanding why queues are often used for scheduling tasks, see e.g. this Wikipedia article and the ones it links to.

For understanding why we should prefer to expose primitives directly, instead of forcing people to get them indirectly through other APIs, see e.g. the extensible web manifesto. For this particular case, you may enjoy #512 or w3ctag/design-reviews#294.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants