我正在尝试为返回promise的对象运行测试套件.我希望将几个动作连在一起,并在它们之间进行短暂的超时.我认为返回承诺的"然后"调用将等待承诺在触发下一个链接然后调用之前完成.
我创建了一个函数
function promiseTimeout (time) {
return new Promise(function(resolve,reject){
setTimeout(function(){resolve(time);},time);
});
};
Run Code Online (Sandbox Code Playgroud)
尝试在承诺中包装setTimeout.
然后在我的测试套件中,我正在调用这样的东西......
it('should restore state when browser back button is used',function(done){
r.domOK().then(function(){
xh.fire('akc-route-change','/user/4/profile/new');
}).then(promiseTimeout(2000)).then(function(t){
xu.fire('akc-route-change','/user/6');
}).then(promiseTimeout(10)).then(function(t){
expect(xu.params[0]).to.equal(6);
history.back();
}).then(promiseTimeout(10)).then(function(){
expect(xu.params[0]).to.equal(4);
done();
});
});
Run Code Online (Sandbox Code Playgroud)
我可以在第一个xh.fire呼叫上设置断点,在呼叫时设置第二个xu.fire呼叫,并且当从第一个断点继续到第二个断点时,预期会有两秒的间隙.
相反,它立即到达第二个断点,并且该t点的值未定义.
我究竟做错了什么?