这是我的代码:
return this.myService.getSomeThing()
.then(someData => {
return this.myService.getAll(someData.id)
});
}).then(response => {
// do something;
})
如何在timeout
和getAll
上致电getSomeThing
?
我是角色的新手,但是在超时的情况下我需要重新路由到其他视图(另一个html模板)。
答案 0 :(得分:1)
我认为Promise.race
在这里运作良好。有些事情(我没有测试过,这只是一个想法)。
Promise.race([
this.myService.getSomeThing()
.then(someData => this.myService.getAll(someData.id)),
new Promise( (y, n) => setTimeout( () => y('Timeout fired before completion of Service call'), 1000)
]).then( (resp) => console.log(resp));
这里的想法是等待1000ms。如果service calls
在此之前完成,则最后.then
将显示其结果,否则将显示超时消息。