- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
(async function() {
var a,b;
function flush(){
return new Promise(res => {
res(123)
})}
Promise.resolve().then(() => a = 1)
Promise.resolve().then(() => b = 2)
await flush();
console.log(a)
console.log(b)
})()
在此代码段中,a
和 b
的值记录在控制台中。
(async function() {
var a;
var b;
function flush(){
return new Promise(res => {
res(123)
})}
Promise.resolve().then(() => a = 1).then(() => b = 2)
await flush();
console.log(a)
console.log(b)
})()
在这种情况下,a
的值记录为 1,而 b
未定义。
(async function() {
var a;
var b;
function flush(){
return new Promise(res => {
setTimeout(res)
})}
Promise.resolve().then(() => a = 1).then(() => b = 2)
await flush();
console.log(a)
console.log(b)
})()
这给出了与第一个代码段相同的结果,其中值a
为1,b
为2
我想了解,为什么 promise 链的行为与多个单独的 promise 不同
PS:我对微任务队列和事件循环有基本的了解。
最佳答案
运行 Node 12.3.1,我可以重现问题中所述的观察结果,将 setTimeout(res(123))
更改为 setTimeout(() => res(123))
后。
在 JavaScript 中,并发模型是事件循环,其中单个线程执行队列中的回调。
<小时/>在第一个片段中,发生以下情况。
.then
将回调 () => a = 1
添加到队列中。() => b = 2
已添加到队列中。() => console.log(a); console.log(b)
2 被添加到队列中。a
设置为 1b
设置为 2a
和 b
已记录。由于设置变量是在打印变量之前进行的,因此您会看到 1 和 2。
<小时/>在第二个片段中:
() => a = 1
被.then
添加到队列中.then
返回一个新的 Promise,该 Promise 尚未解析,因为第一个回调尚未运行。然后第二个 .then
将 () => b = 2
附加到待处理的 Promise。() => console.log(a); console.log(b)
被添加到队列中。() => a = 1
运行,并履行第 2 步中创建的 Promise。这会导致添加 () => b = 2
到队列中。a
和 b
已记录。b = 2
已运行,但这发生在 b
被打印之后,而 b
是未定义的
。但是在 Firefox 中,所有三个片段的输出都是相同的。我设法通过添加异步来产生上述行为。Promise.resolve().then(async () => a = 1).then(() => b = 2)
这是一个显示相同问题的简化版本。Node 中为 1 5 2 3 4,Firefox 中为 1 2 3 5 4。
(async function() {
Promise.resolve()
.then(() => console.log(1))
.then(() => console.log(2))
.then(() => console.log(3))
.then(() => console.log(4))
await Promise.resolve()
console.log(5)
})()
但是如果将 await
更改为 .then
,Promise.resolve().then(() => console.log(5))
您在两个平台上都获得 1 5 2 3 4。3
为什么?我用谷歌搜索并找到了这个: https://v8.dev/blog/fast-asyncNode 12 通过 await
优化了一些额外的步骤,这之前需要额外的一次性 promise 和两个微标记。这似乎就是“5”在 Node 12 中提前两步的原因。
await
将其余代码转换为回调。.then
和 await
毕竟是不同的。关于javascript - 链式 promise 如何在微任务队列中排队,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57605016/
一晃五年没写博客了,依旧再C#上耕耘,依旧没有啥建树,现在也不知道.net上还有多少人再使用,在这里分享一些自己觉得写的还算优雅的代码。 对于自己写着完的代码,我特别喜欢链式(来源于jQuer
我正在构建一个吉他和弦查找应用程序。我使用多维数组来表示指板。数组中的每个元素都由具有字符串属性“Note”的 FretSpace 结构表示。为了初始化指板上的音符属性,我传递了要处理的吉他弦的详细信
我在演示代码中使用 setTimeout 函数模拟了 3 个 ajax 调用。我将从一段运行良好的代码开始:所有调用都是并行进行的,我希望所有调用都能成功,否则会出现错误。 var p1 = func
谁能解释一下? a = [2,3,4] b = [5,6,8,9] print(len(a) > 0) print(len(b) > 0) print((len(a) > 0) & len(b) >
我正在处理具有多个子 JSONObject 的 JSONObject。这是我填写内容的方式: myJson.getJSONObject(CAT_NAME).put(VAR_NAME, var)
想象一下这种情况,我有一个需要检查属性的对象。但是,该对象当前可以具有空值。 如何在一个“if”条件下检查这两个条件? 目前,我必须做这样的事情: if (myObject != null) {
我有一个对象集合,称它们为obj。他们有一个 act() 方法。 act() 方法最终会导致 o 上的 event() observable 调用 onComplete。 链接这些的好方法是什么? 即
假设我有一个列表变量 datalist 存储 10,000 个字符串实体。QTableView 只需要显示其中的一些实体。这就是为什么 QTableView 被指定为 QSortFilterProxy
我正在寻找支持链式 MSI 安装的工具(最好不是 InstallShield,而且最好是便宜/免费的)。我有几个小型安装需要能够单独部署,但也需要作为一个组部署,我不想维护多个安装程序。 看起来我需要
在这种情况下,我想迭代集合中除最后 2 个元素之外的所有元素。 假设我采用了一种奇怪的方式,例如 x.Reverse().Skip(2).Reverse()。 每个 LINQ 操作是否会有效地生成一个
对于javascript来说非常陌生,我有两个html数字选择,包括年份,我想将第二个选择与第一个选择链接起来,这样当我在第一个选择中选择年份时(而第二个选择没有选项)首先),第二个选择应包括从所选数
有人可以向我解释一下为什么以下两个链式函数: // returns zero if okay var resetCounter = function (model) { return new Prom
所以我有 2 个 promise 函数。当第一个函数出现错误时,我希望它显示错误消息。当完成或失败时,我希望他们执行一个finally catch all 函数,但由于某种原因它不起作用。我的代码如下
我有一个函数 const func = () => server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyN
(async function() { var a,b; function flush(){ return new Promise(res => {
这个问题已经有答案了: Promise chaining: Use result from previous promise in next then callback [duplicate] (1
这可能不是专业正则表达式理解的问题。唯一重要的是因为我正在运行多个链式替换命令,这些命令会影响文本文件中的某些相同文本。我还想象在替换之前,根据分隔符词(需要多次替换)的使用方式对 txt 文件进行分
我正在尝试构建一组类来定义 OSI 堆栈中协议(protocol)的分层属性...从抽象意义上讲,我只需要从父 python 类继承属性,但我需要能够调用整个类链一次...所以,我正在寻找这样的东西.
我正在努力兑现 promise ,到目前为止我偶然发现了这一点: new Promise((resolve, reject) => { setTimeout(() => { r
我试图理解 promise ,我需要链接它们并装饰来自不同端点的对象宽度数据。 例如: 我的 Node-express 应用程序中有这个 //controller.js export const ge
我是一名优秀的程序员,十分优秀!