茉莉花节点asynchronous不明白代码错误
我正在尝试与asynchronous茉莉节点:
it('should not be dumb', function(done){ Promise.resolve(0).then(function(){ console.log('dumb'); expect(2).toEqual(2); done(); });
});
返回:
dumb . Finished in 0.026 seconds 1 test, 1 assertion, 0 failures, 0 skipped
但不知何故,如果我在我的代码中有一个错误:
it('should not be dumb', function(done){ Promise.resolve(0).then(function(result){ console.log('dumb'); expect(2,toEqual(2)); done(); }, function (err){console.log(err); done()}); });
它只是坐在那里,直到它超时,没有任何有用的输出:
dumb Pairing user stories - 7816 ms should not be dumb - 7815 ms Failures: 1) Pairing user stories should not be dumb Message: timeout: timed out after 5000 msec waiting for spec to complete Stacktrace: undefined Finished in 46.884 seconds 1 test, 1 assertion, 1 failure, 0 skipped
我究竟做错了什么?
假设:
promise.then(onResolve, onReject)
当promise
被拒绝时, onReject
将被调用。 但是,在你的代码中,拒绝发生在onResolve
(隐含地,抛出一个错误),这不会影响promise
(这已经被解决了),因此不会调用onReject
。
如果您想要捕获在onResolve
中引发的onResolve
,则需要通过在您的承诺链中添加另一个拒绝处理程序来处理它们:
Promise.resolve(0).then(function(result){ console.log('dumb'); expect(2,toEqual(2)); done(); }).then(null, function (err) { console.log(err); done() });
如果Promise
恰好是bluebird
,可以使用.catch()
稍微缩短它:
Promise.resolve(0).then(function(result){ console.log('dumb'); expect(2,toEqual(2)); done(); }).catch(function (err) { console.log(err); done() });