【发布时间】:2016-10-18 07:46:05
【问题描述】:
我有一个基于 Promise 的库(适用于 Node.js 0.10 - 6.x),其方法使用 Array 拒绝。
当使用Bluebird 时,会导致警告:a promise was rejected with a non-error。
将数组包装成自定义错误类型很容易,但我想避免破坏库的向后兼容性。
是否有可能实现这样一个可以用作数组的对象,同时被Bluebird 视为Error 对象?
附加内容
当从 Error 继承时,我使用以下帮助程序与 Node.js 0.10 - 0.12 兼容:
function inherits(child, parent) {
child.prototype.__proto__ = parent.prototype;
}
看看Bluebird 来源,也许有办法以某种方式规避其验证:
Promise.prototype._rejectCallback =
function(reason, synchronous, ignoreNonErrorWarnings) {
var trace = util.ensureErrorObject(reason);
var hasStack = trace === reason;
if (!hasStack && !ignoreNonErrorWarnings && debug.warnings()) {
var message = "a promise was rejected with a non-error: " +
util.classString(reason);
this._warn(message, true);
}
this._attachExtraTrace(trace, synchronous ? hasStack : false);
this._reject(reason);
};
【问题讨论】:
标签: javascript arrays node.js promise bluebird