如何在angular2中进行连续的http api调用?

时间:2018-03-10 10:17:37

标签: javascript angular typescript rxjs

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

2 个答案:

答案 0 :(得分:1)

this.serviceInst.firstAPIMethod()
    .flatMap(firstMethodResult => this.serviceInst.secondAPIMethod(firstMethodResult))
    .flatMap(secondMethodResult => this.serviceInst.thirdAPIMethod(secondMethodResult))
    .subscribe(thirdMethodResult => {
          console.log(thirdMethodResult);
     });

答案 1 :(得分:0)

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');
});