触发循环内的同步调用,直到两个api调用成功,然后下一次迭代需要以角度6开始

问题描述 投票:3回答:3

[使用以下代码尝试过,不要等到响应成功之前,调用成功就跳到下一个迭代。要求:成功两次调用api(POST / PATCH)之后需要进行下一次迭代

 for (item of data) {
  A(item)

}
A(value) {
  const resp = this.post(url, {
      'rationale': value['rationale']
    })
    .mergeMap(tempObj => {
      value['detail'] = tempObj['id']
      return this.patch(url, value['extid'], value)
    })
    .subscribe()
}
javascript angular ecmascript-6 rxjs angular6
3个回答
1
投票
// This must be executed inside an async function for (item of data) { await A(item) } async A(value) { const resp = await this.post(url, { 'rationale': value['rationale'] }) .mergeMap(tempObj => { value['detail'] = tempObj['id'] return this.patch(url, value['extid'], value) }).toPromise(); }

0
投票
const resp$ = from(data).pipe( concatMap(value => this.post(url, { 'rationale': value['rationale'] }).pipe( switchMap(tempObj => { value['detail'] = tempObj['id'] return this.patch(url, value['extid'], value) }) )) )

我使用switchMap而不是mergeMap表示未使用mergeMap同时运行多个Observable的功能。


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.