我正在写信订阅循环中的 API 调用。但我希望循环的下一次迭代在收到前一个调用的数据时开始。
我正在编写下面的代码,但它立即发起了 10 个调用。但我想要一个接一个,第一个完成后第二个。
我的代码:
for(let i=0; i<10 ;i++){
this.service.getData().subscribe(data=>{
console('data received for:',i)
});
}
最佳答案
你可以尝试这样,收到数据后再次调用该函数,它会一直持续到计数器小于10 全局变量:
counter = 1;
exampleFunction(){
if(this.counter < 10){
this.service.getData().subscribe(data=>{
if(data){
console('data received for: counter)
this.counter++
this.exampleFunction()
}
});
}
}
关于angular - 想要循环订阅API调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63171626/