“事件循环队列”之间的区别是什么?和“工作队列”? [英] What is the difference between "event loop queue" and "job queue"?

查看:189
本文介绍了“事件循环队列”之间的区别是什么?和“工作队列”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解以下代码是如何运行的。为什么1在b之后但h在3之后?订单应该是:a,b,1,2,h,3?有些文章说事件循环队列和作业队列之间的区别导致了以下输出。但是怎么样?我已阅读 ECMAScript 2015 - 8.4的规范工作和工作队列,想知道Promise'job是如何工作的,但这让我更加困惑。有人能帮我吗?谢谢!

I can not understand how the following code run. Why "1" is after "b" but "h" is after "3"? Should'n the order is: a, b, 1, 2, h, 3? Some article said that the difference between "event loop queue" and "job queue" leads to following output. But how? I have read the specification of ECMAScript 2015 - 8.4 Jobs and Job Queues, wanting to know how Promise'job works, but it makes me more confused. Can someone help me? Thank you!

var promise = new Promise(function(resolve, reject) {resolve(1)});
promise.then(function(resolve) {console.log(1)});
console.log('a');
promise.then(function(resolve) {console.log(2);});
setTimeout(function() {console.log('h')}, 0);
promise.then(function(resolve) {console.log(3)});
console.log('b');

// a
// b
// 1
// 2
// 3
// h

我知道Promise是异步的,但setTimeout(..)异步操作的回调总是在Promise的异步操作之后。为什么?

I know Promise is asynchronous, but the callback of setTimeout(..) asynchronous operation is always after Promise's asynchronous operation. Why?

推荐答案


为什么1在b之后?

Why "1" is after "b"?

通过promise规范,所有promise .then()处理程序在JS的当前线程之后异步调用已经完成了。因此, a b 同时执行,因为当前JS的一部分将在任何<$ c $之前执行c> .then()处理程序所以 1 将始终位于 a 和<$之后c $ c> b 。

By promise specification, all promise .then() handlers are called asynchronously AFTER the current thread of JS has run to completion. Thus, both a and b which are executed synchronously as part of the current JS will execute before any .then() handlers so 1 will always be after a and b.

一些有趣的读物:T 询问,微任务,队列和日程安排 javascript promises中的执行顺序是什么编写JavaScript框架 - 执行时间,超出setTimeout

Some interesting reading: Tasks, microtasks, queues and schedules and What is the order of execution in javascript promises and Writing a JavaScript framework - Execution timing, beyond setTimeout.

这个帖子里有一些很好的建议:承诺在两者之间摇摆不定 nextTick setImmediate

There's some good advice here in this thread:Promises wiggle their way between nextTick and setImmediate:


我不建议依赖
非链式事件的确切执行顺序。如果你想控制执行顺序 -
以一种方式重新排列回调,以便你以后执行
的那个取决于你想要执行的那个
之前,或实现一个队列(在幕后做同样的事情)。

I would not recommend relying on the exact execution order of non-chained events. If you want to control the execution order — rearrange the callbacks in a way so that the one that you want to be executed later depends on the one that you want to be executed earlier, or implement a queue (that does the same behind the hood).

换句话说,如果你依赖于异步的特定时间事件,那么你实际上应该将它们链接在你的代码中,这样就必须通过你的代码在另一个之后发生,而不是依赖于实现中未指定的调度。

In other words, if you depend upon a particular timing of asynchronous events, then you should actually chain them in your code them so one must happen after the other via your code rather than relying on unspecified scheduling in the implementation.

这篇关于“事件循环队列”之间的区别是什么?和“工作队列”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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