Как ES6 async / await работает с Observables и потоками, когда они терпят неудачу? - PullRequest
0 голосов
/ 06 мая 2018

Итак, я пытался повторить этот код: https://github.com/sindresorhus/np/blob/370ef638344ab7115c956b75dc2823850084da39/index.js#L16

И это работает. Однако, если обещание не выполняется, я получаю предупреждение «необработанное обещание». К чему относится выражение catch к чему-то подобному, если оно вообще есть? Есть ли лучший способ получения такой информации?

(node:48454) UnhandledPromiseRejectionWarning: Error: Command failed: np patch --no-cleanup


    at makeError (/Users/daghassi/git/build/node_modules/execa/index.js:172:9)
    at Promise.all.then.arr (/Users/daghassi/git/build/node_modules/execa/index.js:277:16)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
(node:48454) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 5)
(node:48454) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:48454) UnhandledPromiseRejectionWarning: Error: Command failed: np patch --no-cleanup


    at makeError (/Users/daghassi/git/build/node_modules/execa/index.js:172:9)
    at Promise.all.then.arr (/Users/daghassi/git/build/node_modules/execa/index.js:277:16)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:160:7)
(node:48454) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)
(node:48454) UnhandledPromiseRejectionWarning: Error: Command failed: np patch --no-cleanup

1 Ответ

0 голосов
/ 07 мая 2018

Просто поместите блок try / catch вокруг вашего ожидающего вызова. Это должно быть так же, как если бы вы вызывали Promise и предоставляли функцию catch.

Так что если у вас есть функция, которая getPromise() возвращает обещание, вы просто делаете это.

async function asyncFunction() {
    try {
        const result = await getPromise();
    } catch (error) {
        // Add your error handling code here
    }
}
...