async/await是如何捕获异常的?

function 猜大小(猜测) {
  return new Promise((resolve, reject) => {
    // 背下来     console.log("开始摇色子");
    setTimeout(() => {
      let n = 6; //parseInt(Math.random() * 6 + 1, 10); // 1~6       if (n > 3) {
        if (猜测 === "大") {
          resolve(n);
        } else {
          reject(n);
        }
      } else {
        if (猜测 === "小") {
          resolve(n);
        } else {
          reject(n);
        }
      }
    }, 1000);
  });
}

async function test() {
  try {
    let n /*[6,6]*/ = await Promise.all([猜大小("大"), 猜大小("大")]);
    console.log("好嗨哦" + n);
  } catch (error) {
    console.log("输光了" + error);
  }
}

var result = test();
console.log(result);

你可能感兴趣的:(async-await)