async/await怎么抛出异常
 发布于 7 年前  作者 DavidLuman  4110 次预览  最后一次回复是 7 年前  来自 问答 

var method = return new Promise((resolve, reject) =>{。。。。。} var f1 = async function() {await method}, var f2 = async function() { f1() } 如果不在f1的await method这里进行try/catch,那么怎么把异常继续往上抛,让f2来进行try/catch 直接在f2进行try/catch会报UnhandledPromiseRejectionWarning

4 回复
dislido
async function f2() {
  try {
    await f1();
  } catch {
    console.log('catch!');
  }
}

只有用await时才会被try/catch捕获,否则它就是一个普通的promise

async function f2() {
    f1().catch(() => console.log('catch!'));
}
ProfutW

await f1()

来自酷炫的 CNodeMD