Promise.then工作执行顺序 [英] Promise.then Job execution order

查看:143
本文介绍了Promise.then工作执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

规范说(第5段):


来自单个作业队列的PendingJob记录始终以
FIFO顺序启动。此规范未定义
多个作业队列的服务顺序。 ECMAScript实现可以
交换作业
队列的PendingJob记录的FIFO评估,并评估一个或多个
其他作业队列的PendingJob记录。

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.

这是否意味着我不能指望在提供回调之前提供给 .then 的回调在其他同步控制流程中 setTimeout

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'));

推荐答案


这是否意味着我不能依靠提供给 .then 在其他同步控制流中提供给 setTimeout 的回调之前进行评估?

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.

但实际上,使用native Promise <的实现/ code>支持我已经测试了它已完成计划的macrotask后立即安排然后回调(来自PendingJobs队列的微任务)它,在其他未决的macrotask之前,即使在微任务之前计划挂起的macrotask。 ( setTimeout 并且事件是macrotasks。)

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

例如,在我测试它的环境中,这个输出 A C B 可靠:

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"));

但JavaScript规范并不要求它。

But the JavaScript spec doesn't require it.

As Bergi指出,对于用户代理环境, HTML5规范涵盖了这个的微任务和宏任务规范。但这仅适用于用户代理环境(如浏览器)。

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 <例如,/ a>不遵循该规范的定义(尤其是因为它的定时器函数返回对象,而不是数字),但Node也给我们 A ,<$ c上面是$ c> C B ,因为(感谢Benjamin Gruenbaum!)它在<$之后运行承诺解决方案c $ c> nextTick 队列但在任何计时器或I / O回调之前。有关详细信息,请参见他的要点

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.

这篇关于Promise.then工作执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆