【问题标题】:How to make sequential http api calls in angular2?如何在 angular2 中进行连续的 http api 调用?
【发布时间】:2018-08-18 20:38:13
【问题描述】:

我有多个 api 调用,其中依赖于另一个调用。如何使用 rxjs 实现这一点?

【问题讨论】:

标签: javascript angular typescript rxjs


【解决方案1】:
this.serviceInst.firstAPIMethod()
    .flatMap(firstMethodResult => this.serviceInst.secondAPIMethod(firstMethodResult))
    .flatMap(secondMethodResult => this.serviceInst.thirdAPIMethod(secondMethodResult))
    .subscribe(thirdMethodResult => {
          console.log(thirdMethodResult);
     });

【讨论】:

    【解决方案2】:
    let inSequence = (tasks) => { // tasks being an array of functions returning promises
      return tasks.reduce((p, task) => p.then(task), Promise.resolve());
    }
    
    inSequence(tasks).then(() => {
      console.log('all requests done');
    });
    

    【讨论】:

      猜你喜欢
      • 2016-04-01
      • 2021-07-29
      • 2019-01-01
      • 2016-06-11
      • 2021-01-13
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 2017-06-19
      相关资源
      最近更新 更多