首页 > 解决方案 > Node.js Promise 不返回

问题描述

我有一个用例来解决 Promise 而不返回它。在内部捕获错误,但不希望调用者等待承诺解决。

doSomething()
{
    Promise.resolve()
        .then(() => {
            // do something.
        })
        .catch(reason => {
            this.logger.error(reason);
        });
}

收到此错误:

(node:2072) Warning: a promise was created in a handler at internal/timers.js:439:21 but was not returned from it, see http://. goo.gl/rRqMUw
    at Function.Promise.cast (.../node_modules/bluebird/js/release/promise.js:225:13)

标签: node.jspromisebluebird

解决方案


只需从创建 Promise 的 Promise 回调中返回一些内容fire and forget

我猜那个处理程序是doSomething

doSomething()
{
    Promise.resolve()
    .then(() => {
        // do something.
    })
    .catch(reason => {
        this.logger.error(reason);
    });

    return null //or anything else that's sensible
}

注意:我们通常会忽略错误消息,但有时它们包含有价值的信息。在您的错误中有一个链接 http://。goo.gl/rRqMUw 准确地解释了这个问题:d


推荐阅读