一句话回答你的问题,
如何修复输出警告?
通过启用长堆栈跟踪。
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.js 文件:
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;
};
请记住,在 Node.js 中,您可以选择使用环境变量为整个进程配置警告和长堆栈跟踪。
进入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
Promise.config(Object {
warnings: boolean=false,
longStackTraces: boolean=false,
cancellation: boolean=false,
monitoring: boolean=false
} options) -> Object;
配置长堆栈跟踪、警告、监控和取消。请注意,即使 false 是此处的默认值,也可能会检测到自动启用长堆栈跟踪和警告的开发环境。
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
}
});
wForgottenReturn 是唯一可以单独配置的警告类型。对应的环境变量key为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
您可能想从 bluebird 的一位贡献者那里查看official source