Hi FE !
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
Ai
git
前端面试题
前端小tip
  • vite
  • webpack
npm
  • vue2
  • vue3
react
GitHub
let list = [1,2,3];

const square = num => {
    return new Promise((resolve,reject) => {
        setTimeout(() => {
            resolve(num * num)
        }, 1000)
    })
}
function test() {
    list.forEach(async x => {
        const res = await square(x);
        console.log(res)
    })
}
test()

一秒时候,同时输出 1 4 9

为什么会这样呢?

因为 forEach 中的函数 相当在while 循环里执行多个匿名函数

(async (x) => { const res = await = square(x); console.log(res) })(1)

(async (x) => { const res = await = square(x); console.log(res) })(2)

(async (x) => { const res = await = square(x); console.log(res) })(3)

他们之间是相互不影响的,所以会同时输出1,4,9

那么如何让改写呢?

方1

async function test() {
    for(let i = 0; i < list.length; i ++){
        const res = await square(list[i])
        console.log(res)
    }
}
await test()

方法 2

async function test() {
    for(let i in list){
        const res = await square(i)
        console.log(res)
    }
}
await test()

方法3

let promise = Promise.resolve();
function test(i) {
    if(i === list.length) return;
    promise = promise.then(async() => {
        const res = await square(list[i]);
        console.log(res)
    })
    test(i + 1)
}
test(0)

Edit this page
最近更新: 2025/6/27 02:24
Contributors: qdleader
qdleader
本站总访问量 129823次 | 本站访客数 12人