TL; DR
ใช้Promise.allสำหรับการเรียกฟังก์ชั่นแบบขนานพฤติกรรมการตอบรับไม่ถูกต้องเมื่อเกิดข้อผิดพลาด
ขั้นแรกให้ดำเนินการเรียกแบบอะซิงโครนัสทั้งหมดในครั้งเดียวและรับPromiseวัตถุทั้งหมด ประการที่สองใช้awaitกับPromiseวัตถุ วิธีนี้ในขณะที่คุณรอก่อนPromiseเพื่อแก้ไขการโทรแบบอะซิงโครนัสอื่น ๆ ยังคงดำเนินอยู่ โดยรวมแล้วคุณจะรอตราบเท่าที่การโทรแบบอะซิงโครนัสช้าที่สุด ตัวอย่างเช่น:
// Begin first call and store promise without waiting
const someResult = someCall();
// Begin second call and store promise without waiting
const anotherResult = anotherCall();
// Now we await for both results, whose async processes have already been started
const finalResult = [await someResult, await anotherResult];
// At this point all calls have been resolved
// Now when accessing someResult| anotherResult,
// you will have a value instead of a promise
ตัวอย่าง JSbin: http://jsbin.com/xerifanima/edit?js,console
Caveat:มันไม่สำคัญว่าการawaitโทรจะอยู่ในสายเดียวกันหรือต่างสายกันตราบใดที่การawaitโทรครั้งแรกเกิดขึ้นหลังจากการโทรแบบอะซิงโครนัสทั้งหมด ดูความคิดเห็นของ JohnnyHK
ปรับปรุง:คำตอบนี้มีระยะเวลาที่แตกต่างกันในการจัดการข้อผิดพลาดตามที่คำตอบของ @ bergiก็ไม่ไม่โยนออกข้อผิดพลาดเป็นข้อผิดพลาดเกิดขึ้น แต่หลังจากที่สัญญาทั้งหมดจะดำเนินการ ฉันเปรียบเทียบผลลัพธ์กับเคล็ดลับของ @ jonny: [result1, result2] = Promise.all([async1(), async2()])ตรวจสอบข้อมูลโค้ดต่อไปนี้
const correctAsync500ms = () => {
  return new Promise(resolve => {
    setTimeout(resolve, 500, 'correct500msResult');
  });
};
const correctAsync100ms = () => {
  return new Promise(resolve => {
    setTimeout(resolve, 100, 'correct100msResult');
  });
};
const rejectAsync100ms = () => {
  return new Promise((resolve, reject) => {
    setTimeout(reject, 100, 'reject100msError');
  });
};
const asyncInArray = async (fun1, fun2) => {
  const label = 'test async functions in array';
  try {
    console.time(label);
    const p1 = fun1();
    const p2 = fun2();
    const result = [await p1, await p2];
    console.timeEnd(label);
  } catch (e) {
    console.error('error is', e);
    console.timeEnd(label);
  }
};
const asyncInPromiseAll = async (fun1, fun2) => {
  const label = 'test async functions with Promise.all';
  try {
    console.time(label);
    let [value1, value2] = await Promise.all([fun1(), fun2()]);
    console.timeEnd(label);
  } catch (e) {
    console.error('error is', e);
    console.timeEnd(label);
  }
};
(async () => {
  console.group('async functions without error');
  console.log('async functions without error: start')
  await asyncInArray(correctAsync500ms, correctAsync100ms);
  await asyncInPromiseAll(correctAsync500ms, correctAsync100ms);
  console.groupEnd();
  console.group('async functions with error');
  console.log('async functions with error: start')
  await asyncInArray(correctAsync500ms, rejectAsync100ms);
  await asyncInPromiseAll(correctAsync500ms, rejectAsync100ms);
  console.groupEnd();
})();