admin管理员组

文章数量:1431419

The spec says (para 5):

The PendingJob records from a single Job Queue are always initiated in FIFO order. This specification does not define the order in which multiple Job Queues are serviced. An ECMAScript implementation may interweave the FIFO evaluation of the PendingJob records of a Job Queue with the evaluation of the PendingJob records of one or more other Job Queues.

Does this mean I can't count on the callback supplied to .then being evaluated before a callback supplied to setTimeout in an otherwise synchronous control flow?

In other words, can I depend on the following printing one two.

setTimeout(() => console.log('two'));
Promise.resolve().then(() => console.log('one'));

The spec says (para 5):

The PendingJob records from a single Job Queue are always initiated in FIFO order. This specification does not define the order in which multiple Job Queues are serviced. An ECMAScript implementation may interweave the FIFO evaluation of the PendingJob records of a Job Queue with the evaluation of the PendingJob records of one or more other Job Queues.

Does this mean I can't count on the callback supplied to .then being evaluated before a callback supplied to setTimeout in an otherwise synchronous control flow?

In other words, can I depend on the following printing one two.

setTimeout(() => console.log('two'));
Promise.resolve().then(() => console.log('one'));

Share Improve this question asked Apr 24, 2017 at 15:34 Ben AstonBen Aston 55.8k69 gold badges220 silver badges349 bronze badges 1
  • related: Difference between microtask and macrotask within an event loop context – Bergi Commented May 8, 2018 at 18:04
Add a ment  | 

3 Answers 3

Reset to default 6

Does this mean I can't count on the callback supplied to .then being evaluated before a callback supplied to setTimeout in an otherwise synchronous control flow?

Yes, that's what it means; the spec doesn't require that implementations work that way.

But in practice, the implementations with native Promise support I've tested it on have scheduled the then callback (a "microtask" from the PendingJobs queue) immediately after finishing the "macrotask" that scheduled it, before other pending macrotasks, even when the pending macrotask was scheduled before the microtask. (setTimeout and events are macrotasks.)

E.g., in the environments where I've tested it, this outputs A, C, B reliably:

console.log("A");
setTimeout(_ => console.log("B"), 0);
Promise.resolve().then(_ => console.log("C"));

But the JavaScript spec doesn't require it.

As Bergi points out, for user agent environments, the HTML5 spec covers this in its specification for microtasks and macrotasks. But that's only applicable to user agent environments (like browsers).

Node doesn't follow that spec's definition, for instance (not least because its timer functions return objects, not numbers), but Node also gives us A, C, B above, because (thanks Benjamin Gruenbaum!) it runs promise resolutions after the nextTick queue but before any timer or I/O callbacks. See his gist for details.

Yes, that's what it means - an other event might fire before the promise callback.

No, that won't happen - while ECMAScript allows it, the setTimeout spec does not.

setTimeout does not mean that the supplied function will be executed after the provided time. It adds the function to the end of the queue once the delay has elapsed.

It really depends on when your promise resolves, as to the execution of the two statements. In your example, setTimeout will add it's callback to the queue ahead of the resolved promise, so you can expect one two.

本文标签: javascriptPromisethen Job execution orderStack Overflow