Promise.all() 中的第一个承诺没有正确执行
First promise inside Promise.all() doesn't execute properly
wait
函数用作睡眠函数,fn
函数接受一个数组(项目),它记录每个项目并在记录下一个项目之前休眠一秒钟。
const wait = async(time) => {
return new Promise((resolve) => setTimeout(resolve, time))
}
const fn = async(items) => {
for (item of items) {
await wait(1000)
console.log(item)
}
}
const exeAll = async() => {
Promise.all([
fn(['A1', 'A2']),
fn(['B1', 'B2']),
])
}
exeAll()
问题是 exeAll
函数提供的结果,它打印:
B1
A2
B2
B2
但我认为它应该打印如下内容:
A1
B1
A2
B2
执行上述代码时,A1根本不显示。谁能解释为什么?
for (item of items) {
将创建一个隐式全局变量 item
,即多次调用 fn
会相互干扰,覆盖 item
。正确声明变量并按预期工作:
const wait = async(time) => {
return new Promise((resolve) => setTimeout(resolve, time))
}
const fn = async(items) => {
for (let item of items) {
// ^^^^
await wait(1000)
console.log(item)
}
}
const exeAll = async() => {
Promise.all([
fn(['A1', 'A2']),
fn(['B1', 'B2']),
])
}
exeAll()
我们可以向 fn
添加更多日志记录,以查看在您的情况下会发生什么:
const wait = async(time) => {
return new Promise((resolve) => setTimeout(resolve, time))
}
let counter = 0;
const fn = async(items) => {
const c = counter++;
console.log(`enter fn call #${c}`);
for (item of items) {
console.log(`fn call #${c} sets item <-`, item);
await wait(1000)
console.log(`fn call #${c} reads item ->`, item);
console.log(item)
}
}
const exeAll = async() => {
Promise.all([
fn(['A1', 'A2']),
fn(['B1', 'B2']),
])
}
exeAll()
严格模式 ("use strict";
) 会发现该错误,因为分配给未声明的变量会引发错误。
wait
函数用作睡眠函数,fn
函数接受一个数组(项目),它记录每个项目并在记录下一个项目之前休眠一秒钟。
const wait = async(time) => {
return new Promise((resolve) => setTimeout(resolve, time))
}
const fn = async(items) => {
for (item of items) {
await wait(1000)
console.log(item)
}
}
const exeAll = async() => {
Promise.all([
fn(['A1', 'A2']),
fn(['B1', 'B2']),
])
}
exeAll()
问题是 exeAll
函数提供的结果,它打印:
B1
A2
B2
B2
但我认为它应该打印如下内容:
A1
B1
A2
B2
执行上述代码时,A1根本不显示。谁能解释为什么?
for (item of items) {
将创建一个隐式全局变量 item
,即多次调用 fn
会相互干扰,覆盖 item
。正确声明变量并按预期工作:
const wait = async(time) => {
return new Promise((resolve) => setTimeout(resolve, time))
}
const fn = async(items) => {
for (let item of items) {
// ^^^^
await wait(1000)
console.log(item)
}
}
const exeAll = async() => {
Promise.all([
fn(['A1', 'A2']),
fn(['B1', 'B2']),
])
}
exeAll()
我们可以向 fn
添加更多日志记录,以查看在您的情况下会发生什么:
const wait = async(time) => {
return new Promise((resolve) => setTimeout(resolve, time))
}
let counter = 0;
const fn = async(items) => {
const c = counter++;
console.log(`enter fn call #${c}`);
for (item of items) {
console.log(`fn call #${c} sets item <-`, item);
await wait(1000)
console.log(`fn call #${c} reads item ->`, item);
console.log(item)
}
}
const exeAll = async() => {
Promise.all([
fn(['A1', 'A2']),
fn(['B1', 'B2']),
])
}
exeAll()
严格模式 ("use strict";
) 会发现该错误,因为分配给未声明的变量会引发错误。