Bluebird 被遗忘 return 警告丢失
Bluebird forgotten return warning is missing
我希望 Bluebird forgotten return warning 出现,但由于某些原因它不起作用。
一个demo:
const Bluebird = require('bluebird');
Bluebird.config({
warnings: true
})
Bluebird.resolve(1)
.then(() => {
Bluebird.resolve(2); // should warn about forgotten return
})
.then(two => console.log(two));
如何解决输出警告?
我怀疑我以前遇到过这个问题,但我不记得解决方案是什么。
似乎需要启用长堆栈跟踪才能显示警告。您可以使用配置对象来启用它们 (docs) (demo):
Bluebird.config({
warnings: true,
longStackTraces: true
});
In Node.js you may configure warnings and long stack traces for the
entire process using environment variables:
BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js
Both features are automatically enabled if the BLUEBIRD_DEBUG
environment variable has been set or if the NODE_ENV environment
variable is equal to "development".
和
To enable long stack traces and warnings in node development:
$ NODE_ENV=development node server.js
To enable long stack traces and warnings in node production:
$ BLUEBIRD_DEBUG=1 node server.js
编辑为什么这是必要的:
似乎默认情况下禁用警告和长堆栈跟踪,并且仅在检测到开发环境时才启用,参见 here:
Note that even though false is the default here, a development environment might be detected which automatically enables long stack traces and warnings.
您可以使用配置对象中 wForgottenReturn
和 longStackTraces
属性的组合来配置检查遗忘 return 语句的警告。 wForgottenReturn
是 warning
的 属性,必须设置为 true,并且是唯一可以单独配置的警告类型。对应的环境变量key为BLUEBIRD_W_FORGOTTEN_RETURN
。您可以查看 documentation 了解更多信息。
const Bluebird = require('bluebird');
Bluebird.config({
warnings: {
wForgottenReturn: true
}, longStackTraces: true,
});
Bluebird.resolve(1).then(() => {
Bluebird.resolve(2);
}).then(two => console.log(two));
运行 控制台中的程序给我:
Warning: a promise was created in a handler at /home/adrianpop/test/bb.js:11:13 but was not returned from it, see
at Function.Promise.cast (/home/adrianpop/Downloads/Test/node_modules/bluebird/js/release/promise.js:196:13)
undefined
这是你想要的输出。
您还可以 运行 申请为:
BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js
,产生相同的结果。
干杯!
编辑:
从 github 上的 this 问题,我们有:
So the problem is that by default Nodejs 6.x does not display stack
traces for warnings. There is a command line option (--trace-warnings)
to enable them. Without this option Bluebird warnings are a lot less
useful. Without the call stack, it can be very difficult to figure out
where the warning originated.
还可以找到更多信息:
一言以蔽之,
How can it be fixed to output a warning?
通过启用长堆栈跟踪。
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
});
您可以配置检查忘记 return 语句的警告 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
我希望 Bluebird forgotten return warning 出现,但由于某些原因它不起作用。
一个demo:
const Bluebird = require('bluebird');
Bluebird.config({
warnings: true
})
Bluebird.resolve(1)
.then(() => {
Bluebird.resolve(2); // should warn about forgotten return
})
.then(two => console.log(two));
如何解决输出警告?
我怀疑我以前遇到过这个问题,但我不记得解决方案是什么。
似乎需要启用长堆栈跟踪才能显示警告。您可以使用配置对象来启用它们 (docs) (demo):
Bluebird.config({
warnings: true,
longStackTraces: true
});
In Node.js you may configure warnings and long stack traces for the entire process using environment variables:
BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js
Both features are automatically enabled if the BLUEBIRD_DEBUG environment variable has been set or if the NODE_ENV environment variable is equal to "development".
和
To enable long stack traces and warnings in node development:
$ NODE_ENV=development node server.js
To enable long stack traces and warnings in node production:
$ BLUEBIRD_DEBUG=1 node server.js
编辑为什么这是必要的:
似乎默认情况下禁用警告和长堆栈跟踪,并且仅在检测到开发环境时才启用,参见 here:
Note that even though false is the default here, a development environment might be detected which automatically enables long stack traces and warnings.
您可以使用配置对象中 wForgottenReturn
和 longStackTraces
属性的组合来配置检查遗忘 return 语句的警告。 wForgottenReturn
是 warning
的 属性,必须设置为 true,并且是唯一可以单独配置的警告类型。对应的环境变量key为BLUEBIRD_W_FORGOTTEN_RETURN
。您可以查看 documentation 了解更多信息。
const Bluebird = require('bluebird');
Bluebird.config({
warnings: {
wForgottenReturn: true
}, longStackTraces: true,
});
Bluebird.resolve(1).then(() => {
Bluebird.resolve(2);
}).then(two => console.log(two));
运行 控制台中的程序给我:
Warning: a promise was created in a handler at /home/adrianpop/test/bb.js:11:13 but was not returned from it, see
at Function.Promise.cast (/home/adrianpop/Downloads/Test/node_modules/bluebird/js/release/promise.js:196:13)
undefined
这是你想要的输出。
您还可以 运行 申请为:
BLUEBIRD_LONG_STACK_TRACES=1 BLUEBIRD_WARNINGS=1 node app.js
,产生相同的结果。
干杯!
编辑:
从 github 上的 this 问题,我们有:
So the problem is that by default Nodejs 6.x does not display stack traces for warnings. There is a command line option (--trace-warnings) to enable them. Without this option Bluebird warnings are a lot less useful. Without the call stack, it can be very difficult to figure out where the warning originated.
还可以找到更多信息:
一言以蔽之,
How can it be fixed to output a warning?
通过启用长堆栈跟踪。
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
});
您可以配置检查忘记 return 语句的警告 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