运行以下代码时,如果我将
天哪,一个对
以上代码的输出结果为:
console.log("fnError: ", fnError)
注释掉或不注释掉,会得到不同的结果。这对我来说似乎非常奇怪。天哪,一个对
console.log
的调用如何影响我的promise?function run() {
var fn = function(){
throw new Error("incorrect message");
};
// returns a promise that should fail with
// an error object whose .message is "correct message"
var promisifiedFn = function(){
return Promise.resolve()
.then(fn)
.catch((fnError) => {
// commenting this out fixes things! //
console.log("fnError: ", fnError);
///////////////////////////////////////
fnError.message = "correct message";
throw fnError;
})
}
promisifiedFn().catch((e) => {
console.log("caught error.message:", e.message);
console.log("caught error:", e);
});
}
run();
以上代码的输出结果为:
// fnError: Error: incorrect message
// at fn (/Users/sam/dev/ethereum/pennyeth/js/temp.js:18:9)
// at <anonymous>
// at process._tickCallback (internal/process/next_tick.js:169:7)
// ...
// caught error.message: correct message
// caught error: Error: incorrect message
// at fn (/Users/sam/dev/ethereum/pennyeth/js/temp.js:18:9)
// at <anonymous>
// at process._tickCallback (internal/process/next_tick.js:169:7)
// ...
注意到“错误信息”被记录在日志中。如果您取消注释console.log("fnError: ", fnError)
,您将得到以下结果:
// caught error.message: correct message
// caught error: Error: correct message
// at fn (/Users/sam/dev/ethereum/pennyeth/js/temp.js:18:9)
// at <anonymous>
// at process._tickCallback (internal/process/next_tick.js:169:7)
// ....
运行 Node 8.0.0
console.log
,仍然得到错误的错误对象。我不得不抛出一个新的错误,但这会丢失原始的堆栈跟踪。 - JS_Riddlerconsole.log(Object.getOwnPropertyDescriptor(fnError, "message"))
吗? - Bergi{ value: 'first', writable: true, enumerable: false, configurable: true }
- styfle