js在数组map时使用异步

直接上代码:

let arr = [1, 2, 3, 4, 2];

async function xxx(num) {
    
    
    return 666 + num;
}
async function ttt() {
    
    
    let res = await Promise.all(arr.map(async (item) => {
    
    
        return await xxx(item);
    }))
    return res;
}
ttt().then(console.log)

[ 667, 668, 669, 670, 668 ]

参考老铁的方法自己试验后的:https://blog.csdn.net/hannah1116/article/details/86541680

猜你喜欢

转载自blog.csdn.net/kuilaurence/article/details/117931829