- father::数组的map()方法,异步编程
梗概
- map中使用异步函数会将原数组返回套了一层Promise对象的数组, 这时使用await promise.all即可去除promise
示例
const arr = [1, 2, 3];
const asyncMap = async () => {
const results = await Promise.all(arr.map(async (num) => {
// 模拟异步操作
await new Promise(resolve => setTimeout(resolve, 1000));
return num * 2;
}));
console.log(results); // [2, 4, 6]
};
asyncMap();father:: JavaScript