处理中的超时休息请求

时间:2017-07-26 17:38:15

标签: javascript angular

这是我的代码:

  return this.myService.getSomeThing()

    .then(someData => {
                return this.myService.getAll(someData.id)
                });
            }).then(response => { 
                // do something;
           })

如何在timeoutgetAll上致电getSomeThing? 我是角色的新手,但是在超时的情况下我需要重新路由到其他视图(另一个html模板)。

1 个答案:

答案 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将显示其结果,否则将显示超时消息。