我有這個函式,它呼叫一個服務來通過 api 獲取資料。我想在第一次呼叫后,如果結果等于 50,則使用更新的偏移量進行另一個呼叫。但這不起作用。它不會進入箭頭函式:
result: any[] = []
async getData(v) {
await this.gs.getMaxGifs(v, this.offset).subscribe((response: any) => {
this.result.push(...response.data);
this.offset = 50;
if (this.result.length % 50 == 0) {
console.log('DENTRO')
async () => {
await this.gs.getMaxGifs(v, this.offset).subscribe((response: any) => {
console.log('DENTRO22222')
})
this.result.push(...response.data);
this.offset = 50;
}
}
console.log(this.result.length)
});
}

當這有效時,我想用while替換if。如果我現在插入它會進入無限回圈
uj5u.com熱心網友回復:
正如評論它對先生有用,
async getData(v) {
await this.gs.getMaxGifs(v, this.offset).subscribe((response: any) => { this.result.push(...response.data);
this.offset = 50; if (this.result.length % 50 == 0) {
console.log('DENTRO');
this.getData(v);
}
console.log(this.result.length) });
uj5u.com熱心網友回復:
result: any[] = []
getData(v) {
this.gs.getMaxGifs(v,
this.offset).subscribe((response: any) => {
this.result.push(...response.data);
this.offset = 50;
if (this.result.length % 50 == 0) {
console.log('DENTRO')
this.gs.getMaxGifs(v, this.offset).subscribe((response: any) => {
console.log('DENTRO22222')
this.result.push(...response.data);
this.offset = 50;
}
}
console.log(this.result.length)
});
}
或者,如果你想這樣做遞回,你可以這樣做
result: any[] = []
getData(v) {
this.gs.getMaxGifs(v,
this.offset).subscribe((response: any) => {
this.result.push(...response.data);
this.offset = 50;
if (this.result.length % 50 == 0) {
this.getData(v)
}
console.log(this.result.length)
});
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/392320.html
上一篇:如何為SQLite插入陳述句轉義雙引號-Angular
下一篇:不能在陣列中NgFor
