为什么我没有收到“在处理程序中创建了承诺但没有从中返回"的警告? [英] Why do I not get the warning "a promise was created in a handler but was not returned from it"?

查看:84
本文介绍了为什么我没有收到“在处理程序中创建了承诺但没有从中返回"的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更清楚-在下面的示例中,我没有收到警告.我想知道为什么,并查看示例代码,其中将显示警告.

to be more clear - I'm NOT getting warning in example below. I would like to know why and see example code where warning would be printed.

在我的Node.js项目中,我使用的是 bluebird Promises库.我观察到某些情况下收到以下警告:

In my Node.js project I'm using bluebird Promises library. I observed some cases where I'm getting following warning:

警告:在处理程序中创建了一个诺言,但未从中返回

Warning: a promise was created in a handler but was not returned from it

我知道,当没有与任何承诺链连接的承诺时,就会发生此问题.

I understand that the problem occurs when there is promise that is not connected to any promise chain.

我正在尝试完全理解它的工作原理以及上述情况下的确切问题.为此,我从

I'm trying to fully understand how this works and what exactly the problem is in mentioned case. For that purpose I took example from the documentation.

这是我的代码示例,是基于我没有收到任何警告的文档中的示例得出的:

Here is example of my code based on the example from the documentation for which I'm not getting any warning:

const Promise = require('bluebird');

function getUser() {
    return new Promise(function (resolve, reject) {
        setTimeout(function () {
            console.log('get user');
            resolve('userId');
        }, 4000);
    });
}

function getUserData(userId) {
    new Promise(function (resolve, reject) {
        setTimeout(function () {
            console.log('get user data');
            resolve('userData');
        }, 5000);
    });
}

getUser().then(function(user) {
    getUserData(user);
}).then(function(userData) {
    // userData is undefined as expected
    console.log('Data: ', userData);
});

控制台输出:

/usr/local/opt/node@6/bin/node test.js
get user
Data:  undefined
get user data

Process finished with exit code 0

有人知道为什么我的案子没有警告吗,以及如何重现它?

Does anyone know why there is no warning in my case and how to reproduce it?

我正在使用Node v6.11.3和Bluebird 3.1.1

I'm using Node v6.11.3 and Bluebird 3.1.1

基于Pierre Clocher的回答,我也尝试了以下操作:

getUser().then(function(user) {
    getUserData(user);
}).then(function(userData) {
    console.log('Data: ', userData);
    // userData is undefined
    throw new Error();
}).catch(function (error) {
    console.log('error: ', error);
});

也没有任何警告.

控制台输出:

/usr/local/opt/node@6/bin/node test.js
get user
Data:  undefined
error:  Error
    at test.js:64:11
    at tryCatcher (/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (/node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (/node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (/node_modules/bluebird/js/release/promise.js:693:18)
    at Async._drainQueue (/node_modules/bluebird/js/release/async.js:133:16)
    at Async._drainQueues (/node_modules/bluebird/js/release/async.js:143:10)
    at Immediate.Async.drainQueues (/node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:672:20)
    at tryOnImmediate (timers.js:645:5)
    at processImmediate [as _immediateCallback] (timers.js:617:5)
get user data

Process finished with exit code 0

检查Bluebird.js源代码

如果有人更熟悉Bluebird的源代码,我已经在源代码中进行了搜索,并找到了检查警告的地方(

If someone is more familiar with Bluebird source, i've searched across source and find where there is check for warning (link to source):

if (returnValue === undefined && promiseCreated !== null &&
        wForgottenReturn) {
....

}
var msg = "a promise was created in a " + name +
    "handler " + handlerLine + "but was not returned from it, " +
    "see <link to goo.gl removed>" +
    creatorLine;
promise._warn(msg, true, promiseCreated);

使用我检查过的调试器,在我的情况下 returnValue 未定义, wForgottenReturn 标志设置为true,但 promiseCreated 为空,因此警告为不打印.在我的示例中,我会像通过getUserData(user)的Promise那样解释为未创建/未处理.

Using debugger I checked, returnValue was undefined in my case, wForgottenReturn flag was set to true but promiseCreated was null and hence warning was not printed. I would interpret this like Promise via getUserData(user) in my example was not detected as created/unhandled.

推荐答案

我会回答自己的问题,因为我发现了问题所在.

I will answer my own question because I found where the catch is.

简短的回答:问题是只有在配置中启用警告后,才会在控制台中打印警告.

说明:

在网上搜索时,我在Bluebird github上发现了问题.在示例代码中,我看到他们使用以下代码手动更新了Bluebird配置:

While searching on web I found this issue reported on Bluebird github. In the example code I saw that they manually updated Bluebird config using following code:

Promise.config({
    warnings: true,
    longStackTraces: true
});

在运行代码之前,我做了同样的事情:

I did the same before running my code:

Promise.config({
    warnings: true,
    longStackTraces: true
});

function getUser() {
    ...
}

function getUserData(userId) {
    ...
}

getUser().then(function(user) {
    getUserData(user);
}).then(function(userData) {
    // userData is undefined as expected
    console.log('Data: ', userData);
});

最后输出警告:

/usr/local/opt/node@6/bin/node test.js
get user
(node:96276) Warning: a promise was created in a handler at test.js:71:5 but was not returned from it, see <goo.gl link removed>
    at new Promise (node_modules/bluebird/js/release/promise.js:79:10)
Data:  undefined
get user data

Process finished with exit code 0

进一步检查,我还发现如果NODE_ENV变量设置为"development",则无需手动设置Bluebird配置以查看警告.在开发"的情况下,此类功能会自动启用. 此API参考.

Checking further I also find that there is no need to manually set Bluebird configuration to see warnings in case if NODE_ENV variable is set to "development". In case of "development" such features are enabled automatically. This is also described in this API reference.

这篇关于为什么我没有收到“在处理程序中创建了承诺但没有从中返回"的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆