javascript - 尽管有几个 `try` -`catch` block ,但获取 UnhandledPromiseRejectionWarning

标签 javascript node.js promise async-await

我正在使用环绕其 API 的第 3 方模块。我有以下代码:

const api = require('3rdpartyapi');

 async function callAPI(params) {
  try {
    let result = await api.call(params);
    return result;
  }
  catch(err) {
    throw err;  //will handle in other function
  }
 }
 
 async function doSomething() {
  try {
    //...do stuff
    let result = await callAPI({a:2,b:7});
    console.log(result);
  }
  catch(err) {
    console.error('oh no!', err);
  }
}

尽管 try-catch block ,第 3 方 API,当它失去与 homebase 的连接时(经常发生 :( ),爆炸:

(node:13128) UnhandledPromiseRejectionWarning: FetchError: request to https://www.example.com failed, reason: getaddrinfo ENOTFOUND

其次是:

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: 1)

为什么我的 try-catch 都没有捕捉到这个?到底什么是未处理的,如何实际处理?

最佳答案

关键是 await 只将“第一层”拒绝转换为错误,但是 promise 可以在里面有 promise,他们的库可能无法捕获里面的拒绝。我已经制作了一个概念证明 3rdpartyapi,它可以触发您看到的行为:


(async function () {

// mock 3rdpartyapi
var api = {
    call: async function(){
        await new Promise((resolve, reject) => {
            // why wrap a promise here? but i don't know
            new Promise((innerResolve, innerReject) => {
                innerReject('very sad'); // unfortunately this inner promise fail
                reject('this sadness can bubble up');
            })
        })
    }
};

// your original code
async function callAPI(params) {
    try {
        let result = await api.call(params);
        return result;
    } catch (err) {
        throw err; //will handle in other function
    }
}

async function doSomething() {
    try {
        //...do stuff
        let result = await callAPI({
            a: 2,
            b: 7
        });
        console.log(result);
    } catch (err) {
        console.error('oh no!', err);
    }
}

doSomething();

})();

输出:

$ node start.js
oh no! this sadness can bubble up
(node:17688) UnhandledPromiseRejectionWarning: very sad
(node:17688) 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: 1)
(node:17688) [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.

关于javascript - 尽管有几个 `try` -`catch` block ,但获取 UnhandledPromiseRejectionWarning,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54699731/

相关文章:

javascript - 如何在运行其余代码之前在工厂中运行http请求

javascript - 如何在html中显示ajax

javascript - 由于滑动功能,UC 浏览器上的水平滚动不起作用

javascript - 带有 promise/async-await 的回调上下文

node.js - 带有静态的 express-rate-limit

node.js - Mongoose 从不连接到 mongodb

javascript - Bluebird — 在处理程序中创建了一个 promise ,但没有从中返回

javascript - 附加到 javascript 数组中的所有值

javascript - 我的加载 gif 动画不工作

javascript - 使用 Puppeteer 时等待文本出现