我正在实现我自己的 Promise A+ 兼容包只是为了好玩。在测试它时,遇到了错误,我很难解决它。
我正在尝试针对 Promise A+ Test Suite 对其进行测试.导致问题的测试用例都是测试“如果/何时 y
是一个 thenable 的 thenable ...”(描述 here)。
这是第一个失败测试的描述:
Promise Promises/A+ Test 2.3.3: Otherwise, if
x
is an object or function, 2.3.3.3: Ifthen
is a function, call it withx
asthis
, first argumentresolvePromise
, and second argumentrejectPromise
2.3.3.3.1: If/whenresolvePromise
is called with valuey
, run[[Resolve]](promise, y)
y
is a thenable for a thenabley
is an already-fulfilled promise for a synchronously-fulfilled custom thenablethen
callsresolvePromise
synchronously via return from a fulfilled promise
这就是我编写适配器的方式:
var adapter = (function() {
var res, rej;
return {
defered: {
promise: new Promise(function(resolve, reject){
res = resolve;
rej = reject;
}),
resolve: res,
reject: rej,
}
}
})();
describe("Promises/A+ Tests", function () {
require("promises-aplus-tests").mocha(adapter);
});
这是 gist为全面实现。问题发生在以下部分:
Promise.prototype.then = function(onFulfill, onReject) {
let p = new Promise();
debugger;
let pending = {
fulfill: isFunction(onFulfill) ? onFulfill : v => v,
reject: isFunction(onReject) ? onReject : e => { throw e },
promise: p,
};
this.queue.push(pending);
run.call(this);
return p;
};
这是奇怪的开始。其中一项测试试图评估当一个 promise 产生对一个 promise 的 promise 时,它将以相同的值(value)实现。
我遇到了一些奇怪的错误,所以我一直尝试调试它。
发生了什么:当调试器到达上面代码中的断点时,watch
表现异常。
如果我运行 watch('onFulfill')
,watchers
会给我:
0: onFulfill = undefined
但是如果我执行 exec console.log(onFulfill)
,它将打印:
< [Function: onBasePromiseFulfilled]
这是怎么回事?
编辑:
当在 debugger
语句中停止时,创建了 p
变量,如果我输入:
> p
在调试器控制台中,它给了我:
< { state: State('pending'), queue: [] }
但如果我尝试这样做:
> exec console.log(p)
程序崩溃,给我一个终止错误。
最佳答案
我试图重现这个,但你的适配器看起来不像他们在 the readme 中描述的那样: adapter.deferred
应该是一个函数。修改您的适配器后,我设法使用 Node 检查器对其进行了调试。
即使在修复适配器之后,我确实在 mocha 调试控制台中遇到了一些奇怪的行为,但这是我第一次使用 node/mocha 进行调试,所以我不知道它是否真的很奇怪。没有终止错误,但尝试 console.log
一些明确定义的变量出现未定义。
此外,在不修改适配器的情况下,运行测试几乎立即失败并显示明确的错误消息。如果您不明白,我们可能以不同的方式运行测试,或者我们的设置有所不同。
这是我使用的适配器:
var adapter = (function() {
var res, rej;
return {
deferred: function() {
return {
promise: new Promise(function(resolve, reject){
res = resolve;
rej = reject;
}),
resolve: res,
reject: rej,
};
}
}
})();
关于javascript - Node 调试: watch expression yields different result from exec console.日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34694940/