我正在深入研究node 7的async/await特性,并不断遇到这样的代码

函数getQuote() { 让我们引用= "Lorem ipsum dolor sit met, consectetur adipiscing elit laborum."; 返回引用; } 异步函数main() { 尝试{ var quote = await getQuote(); console.log(报价); } catch(错误){ console.error(错误); } } main ();

这似乎是唯一可能的解决/拒绝或返回/抛出async/await,然而,v8不优化代码在try/catch块?!

还有其他选择吗?


当前回答

try-catch块的替代方法是await-to-js lib。我经常使用它。 例如:

import to from 'await-to-js';

async function main(callback) {
    const [err,quote] = await to(getQuote());
    
    if(err || !quote) return callback(new Error('No Quote found'));

    callback(null,quote);

}

与try-catch相比,此语法要干净得多。

其他回答

我想这样做:)

const sthError = () => Promise.reject('sth error');

const test = opts => {
  return (async () => {

    // do sth
    await sthError();
    return 'ok';

  })().catch(err => {
    console.error(err); // error will be catched there 
  });
};

test().then(ret => {
  console.log(ret);
});

这类似于用co处理错误

const test = opts => {
  return co(function*() {

    // do sth
    yield sthError();
    return 'ok';

  }).catch(err => {
    console.error(err);
  });
};
async function main() {
  var getQuoteError
  var quote = await getQuote().catch(err => { getQuoteError = err }

  if (getQuoteError) return console.error(err)

  console.log(quote)
}

或者,你可以这样做,而不是声明一个可能的var在顶部持有一个错误

if (quote instanceof Error) {
  // ...
}

不过,如果抛出TypeError或Reference错误之类的错误,这将不起作用。您可以确保它是一个常规错误

async function main() {
  var quote = await getQuote().catch(err => {
    console.error(err)      

    return new Error('Error getting quote')
  })

  if (quote instanceOf Error) return quote // get out of here or do whatever

  console.log(quote)
}

我更倾向于把所有东西都包装在一个大的try-catch块中,在这个块中创建了多个promise,这会使处理特定于创建它的promise的错误变得非常麻烦。另一种方法是多个try-catch块,我认为这同样很麻烦

以我的经验,用这种方式抓是很危险的。在整个堆栈中抛出的任何错误都将被捕获,而不仅仅是来自这个承诺的错误(这可能不是您想要的)。

承诺的第二个参数已经是一个拒绝/失败回调。使用它会更好更安全。

下面是我写的一个typescript类型安全的一行程序来处理这个问题:

function wait<R, E>(promise: Promise<R>): [R | null, E | null] {
  return (promise.then((data: R) => [data, null], (err: E) => [null, err]) as any) as [R, E];
}

// Usage
const [currUser, currUserError] = await wait<GetCurrentUser_user, GetCurrentUser_errors>(
  apiClient.getCurrentUser()
);

更清洁的替代方法如下:

因为事实上,每个异步函数在技术上都是一个承诺

您可以在使用await调用函数时向函数添加捕获

async function a(){
    let error;

    // log the error on the parent
    await b().catch((err)=>console.log('b.failed'))

    // change an error variable
    await c().catch((err)=>{error=true; console.log(err)})

    // return whatever you want
    return error ? d() : null;
}
a().catch(()=>console.log('main program failed'))

不需要try catch,因为所有的承诺错误都被处理了,并且你没有代码错误,你可以在父类中省略它!!

假设你正在使用mongodb,如果有一个错误,你可能更喜欢在调用它的函数中处理它,而不是制作包装器,或使用try catch。

try-catch块的替代方法是await-to-js lib。我经常使用它。 例如:

import to from 'await-to-js';

async function main(callback) {
    const [err,quote] = await to(getQuote());
    
    if(err || !quote) return callback(new Error('No Quote found'));

    callback(null,quote);

}

与try-catch相比,此语法要干净得多。