javascript - Promise.all() 中的第一个承诺没有正确执行
问题描述
该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";
) 会发现该错误,因为分配给未声明的变量会引发错误。
推荐阅读
- ibm-mobilefirst - 使用访问令牌的 HTTPRequest 在 IBM Mobilefirst 8.0.0 中调用资源时出错
- discord.js - 如何进行设置命令 [discord.js]
- android - 如何在空对象引用上解决此问题
- c# - 我如何循环对象数组并使用 Linq 将每个对象的属性添加到变量中?
- c - 如何使用C在netfilter中打印特定IP地址的数据包信息?
- android-emulator - 运行android模拟器后其他程序出现延迟
- typescript - `babel-plugin-react-css-modules` Typescript 类型检查
- angular - IONIC底部带有固定按钮的滚动列表页面
- lua - 在 LUA 中获取范围之间的随机数列表,但每个数字允许 x 组重复项
- html - 拒绝加载图像 'unsafe:data:.image-type;base64, [base64String]'