promise.all 与 async task unit中throw的牵扯。。。。。。。

async function test1() {
  // throw new Error('eeeee')
	console.log('test');
	return 1;
}

async function test2() {
  // throw new Error('error2');
  return 2;
}


const asyncTasks = [1, 2, 3].map(async (x) => {
  if(x === 2){
    // throw new Error('error: 44');
  }
})

console.log(asyncTasks);

(async () => {
  try{
    const a = await Promise.all([1, 2, 3].map(async (x) => {
      if(x === 2){
         throw new Error('error: 44');
      }
    }));
    const a = await Promise.all(asyncTasks);
    console.log(a[0]);
  }catch(e) {
    console.log('e = ', e);
  }
})()
dan

谨记promise.all只是把async tasks做最后的同步,不会把环境带入promise.all中,所以注意在每个async中throw的”执行环境“是否有try catch

你可能感兴趣的:(JavaScript编程笔记)