我正在深入研究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块?!

还有其他选择吗?


当前回答

与Golang中的错误处理类似

因为async/await在底层使用承诺,你可以写一个像这样的小实用函数:

export function catchEm(promise) {
  return promise.then(data => [null, data])
    .catch(err => [err]);
}

然后在需要捕捉一些错误时导入它,并包装返回承诺的async函数。

import catchEm from 'utility';

async performAsyncWork() {
  const [err, data] = await catchEm(asyncFunction(arg1, arg2));
  if (err) {
    // handle errors
  } else {
    // use data
  }
}

其他回答

与Golang中的错误处理类似

因为async/await在底层使用承诺,你可以写一个像这样的小实用函数:

export function catchEm(promise) {
  return promise.then(data => [null, data])
    .catch(err => [err]);
}

然后在需要捕捉一些错误时导入它,并包装返回承诺的async函数。

import catchEm from 'utility';

async performAsyncWork() {
  const [err, data] = await catchEm(asyncFunction(arg1, arg2));
  if (err) {
    // handle errors
  } else {
    // use data
  }
}

不需要像await-to-js这样的库,简单的to-function一行代码(也在其他答案中显示)就可以了:

const to = promise => promise.then(res => [null, res]).catch(err => [err || true, null]);

用法:

async function main()
{
    var [err, quote] = await to(getQuote());
    if(err)
    {
        console.log('warn: Could not get quote.');
    }
    else
    {
        console.log(quote);
    }
}

但是,如果错误导致函数或程序终止,例如:

async function main()
{
    var [err, quote] = await to(getQuote());
    if(err) return console.error(err);
    console.log(quote);
}

那么你也可以简单地让错误从main()自动返回,这是异常的预期目的:

async function main()
{
    var quote = await getQuote();
    console.log(quote);
}

main().catch(err => console.error('error in main():', err));

抛出错误vs返回错误

如果您希望处理一个预期会发生的错误,那么使用throw或reject是一种糟糕的实践。相反,让getQuote()函数总是使用以下任何一种方法来解析:

解决([犯错,结果]) 解决(空) 解决(新的错误(…)) 解析({错误:新的错误(),结果:null}) 等。

抛出错误(或在async:拒绝承诺中的等效)必须仍然是异常。由于异常只在出现问题时发生,而不应该在正常使用期间发生,因此优化不是优先考虑的事项。因此,异常的唯一结果可能是函数的终止,如果没有被捕获,这是默认的行为。

除非您处理的是设计糟糕的第三方库,或者您正在为意想不到的用例使用第三方库函数,否则您可能不应该使用to函数。

我想这样做:)

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);
  });
};

我认为,一个简单的例子是如何使用MDN DOCS的承诺。

作为一个例子,他们使用API Fetch,然后是两种类型,一种是正常的,另一种是混合的,其中async和Promise混合在一起。

简单的例子

async function myFetch() {
  let response = await fetch("coffee.jpg");
  // Added manually a validation and throws an error
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }

  let myBlob = await response.blob();

  let objectURL = URL.createObjectURL(myBlob);
  let image = document.createElement("img");
  image.src = objectURL;
  document.body.appendChild(image);
}

myFetch().catch((e) => {
  // Catches the errors...
  console.log("There has been a problem with your fetch operation: " + e.message);
});

混合方法

由于async关键字将一个函数转换为一个promise,你可以重构你的代码,使用promise和await的混合方法,将函数的后半部分引入一个新块,使其更加灵活:

async function myFetch() {
  // Uses async
  let response = await fetch("coffee.jpg");
  // Added manually a validation and throws an error
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }
  return await response.blob();
}

myFetch()
  .then((blob) => {
    // uses plain promise
    let objectURL = URL.createObjectURL(blob);
    let image = document.createElement("img");
    image.src = objectURL;
    document.body.appendChild(image);
  })
  .catch((e) => console.log(e));

添加错误处理

正常的

async function myFetch() {
  try {
    let response = await fetch("coffee.jpg");

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    }
    let myBlob = await response.blob();
    let objectURL = URL.createObjectURL(myBlob);
    let image = document.createElement("img");
    image.src = objectURL;
    document.body.appendChild(image);
  } catch (e) {
    console.log(e);
  }
}

myFetch();

混合(最好的)

async function myFetch() {
  let response = await fetch("coffee.jpg");
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  }
  return await response.blob();
}

myFetch()
  .then((blob) => {
    let objectURL = URL.createObjectURL(blob);
    let image = document.createElement("img");
    image.src = objectURL;
    document.body.appendChild(image);
  })
  .catch(
    (
      e // Not need a try catch. This will catch it all already!
    ) => console.log(e)
  );

最好的解决方案

给出的最佳解决方案遵循这些原则,但增加了更清晰的答案是:> StackOverflow: try/catch block with async/await 我相信。在这里

function promiseHandle(promise) {
  return promise.then((data) => [null, data]).catch((err) => [err]);
}

async function asyncFunc(param1, param2) {
  const [err, data] = await promiseHandle(expensiveFunction(param1, param2));
  // This just to show, that in this way we can control what is going on..
  if (err || !data) {
    if (err) return Promise.reject(`Error but not data..`);
    return Promise.reject(`Error but not data..`);
  }
  return Promise.resolve(data);
}

更清洁的替代方法如下:

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

您可以在使用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。