我有多次api调用,其中依赖于另一个调用。我如何使用rxjs实现这一目标?
答案 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');
});