用一句话回答你的问题,
如何修复输出警告?
通过使能
长堆栈跟踪
.
const Bluebird = require('bluebird');
Bluebird.config({
warnings: true,
longStackTraces: true
})
Bluebird.resolve(1)
.then(() => {
Bluebird.resolve(2); // should warn about forgotten return
})
.then(two => console.log(two));
现在你应该得到一个
new demo
这将标记此错误:
(node:65) Warning: a promise was created in a handler at evalmachine.<anonymous>:16:14 but was not returned from it, see http://bluebirdjs.com/docs/warning-explanations.html#warning-a-promise-was-created-in-a-handler-but-was-not-returned-from-it
at Function.Promise.cast (/home/runner/node_modules/bluebird/js/release/promise.js:196:13)
undefined
允诺
文件:
Promise.cast = function (obj) {
var ret = tryConvertToPromise(obj);
if (!(ret instanceof Promise)) {
ret = new Promise(INTERNAL);
ret._captureStackTrace(); //promise.js:196:13
ret._setFulfilled();
ret._rejectionHandler0 = obj;
}
return ret;
};
记住,在
J.N.
您可以选择使用环境变量为整个进程配置警告和长堆栈跟踪。
下降到
bluebird
的源代码
var warnings = !!(util.env("BLUEBIRD_WARNINGS") != 0 &&
(debugging || util.env("BLUEBIRD_WARNINGS")));
var longStackTraces = !!(util.env("BLUEBIRD_LONG_STACK_TRACES") != 0 &&
(debugging || util.env("BLUEBIRD_LONG_STACK_TRACES")));
var wForgottenReturn = util.env("BLUEBIRD_W_FORGOTTEN_RETURN") != 0 &&
(warnings || !!util.env("BLUEBIRD_W_FORGOTTEN_RETURN"));
和
Promise.config = function(opts) {
opts = Object(opts);
if ("longStackTraces" in opts) {
if (opts.longStackTraces) {
Promise.longStackTraces();
} else if (!opts.longStackTraces && Promise.hasLongStackTraces()) {
disableLongStackTraces();
}
}
if ("warnings" in opts) {
var warningsOption = opts.warnings;
config.warnings = !!warningsOption;
wForgottenReturn = config.warnings;
if (util.isObject(warningsOption)) {
if ("wForgottenReturn" in warningsOption) {
wForgottenReturn = !!warningsOption.wForgottenReturn;
}
}
}
if ("cancellation" in opts && opts.cancellation && !config.cancellation) {
if (async.haveItemsQueued()) {
throw new Error(
"cannot enable cancellation after promises are in use");
}
Promise.prototype._clearCancellationData =
cancellationClearCancellationData;
Promise.prototype._propagateFrom = cancellationPropagateFrom;
Promise.prototype._onCancel = cancellationOnCancel;
Promise.prototype._setOnCancel = cancellationSetOnCancel;
Promise.prototype._attachCancellationCallback =
cancellationAttachCancellationCallback;
Promise.prototype._execute = cancellationExecute;
propagateFromFunction = cancellationPropagateFrom;
config.cancellation = true;
}
if ("monitoring" in opts) {
if (opts.monitoring && !config.monitoring) {
config.monitoring = true;
Promise.prototype._fireEvent = activeFireEvent;
} else if (!opts.monitoring && config.monitoring) {
config.monitoring = false;
Promise.prototype._fireEvent = defaultFireEvent;
}
}
return Promise;
};
默认情况下
长堆栈跟踪、警告、监视和取消
把它们放在
false
在生产环境中。当在开发环境中打开调试器时,它们将被检测并自动启用。
我建议你通过
bluebird's documentation
再来一次。
允诺配置
Promise.config(Object {
warnings: boolean=false,
longStackTraces: boolean=false,
cancellation: boolean=false,
monitoring: boolean=false
} options) -> Object;
配置长堆栈跟踪、警告、监视和取消。注意,尽管如此
假
如果是这里的默认值,则可能会检测到一个自动启用长堆栈跟踪和警告的开发环境。
Promise.config({
// Enable warnings
warnings: true,
// Enable long stack traces
longStackTraces: true,
// Enable cancellation
cancellation: true,
// Enable monitoring
monitoring: true
});
您可以配置用于检查忘记的返回语句的警告
wForgottenReturn
:
Promise.config({
// Enables all warnings except forgotten return statements.
warnings: {
wForgottenReturn: false
}
});
WFORGOTTRETURN公司
是唯一可以单独配置的警告类型。相应的环境变量键是
BLUEBIRD_W_FORGOTTEN_RETURN
.
在node.js中,可以使用环境变量为整个进程配置警告和长堆栈跟踪:
BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js
如果
BLUEBIRD_DEBUG
已设置环境变量,或者如果
NODE_ENV
环境变量等于
"development"
.
使用价值
0
将显式禁用某个功能,而不考虑调试环境,否则将激活该功能:
# Warnings are disabled despite being in development environment
NODE_ENV=development BLUEBIRD_WARNINGS=0 node app.js
你可能想检查一下这个
official source
来自蓝鸟的贡献者之一