Angular 多个并发POST请求,如果其中一个失败,则不发送任何POST请求

Angular 多个并发POST请求,如果其中一个失败,则不发送任何POST请求,angular,typescript,http,rxjs,Angular,Typescript,Http,Rxjs,所以我想同时发送两个post请求,但我希望这样,如果这两个请求中的任何一个失败(例如,意外错误),那么所有请求实际上都不会通过。 这是假设2个post请求是后端上的独立函数(即:2个独立端点) 我搜索并发现了一些相对类似的问题,但如果一个请求失败,所有的解决方案都不会取消这两个请求,而且其中很多都使用了似乎不推荐使用的函数(mergeMap,forJoin,…) 企图 我尝试了多种选择: 将请求映射到另一个请求中: 嵌套订阅: (我知道这不是好的做法) 现在,我把它们完全分开,因为我不清楚如何把

所以我想同时发送两个post请求,但我希望这样,如果这两个请求中的任何一个失败(例如,意外错误),那么所有请求实际上都不会通过。 这是假设2个post请求是后端上的独立函数(即:2个独立端点)

我搜索并发现了一些相对类似的问题,但如果一个请求失败,所有的解决方案都不会取消这两个请求,而且其中很多都使用了似乎不推荐使用的函数(
mergeMap
forJoin
,…)

企图 我尝试了多种选择:

将请求映射到另一个请求中: 嵌套订阅: (我知道这不是好的做法)

现在,我把它们完全分开,因为我不清楚如何把它们联系在一起,记住“如果一个失败了,它们都失败了”

也许我只需要修改后端并将2个post请求作为一个函数(1个端点)处理,但我想知道是否有一种方法可以处理单独的请求。

你说的是什么意思

所有请求实际上都没有通过

后端已经收到这些请求,处理它们并作出响应。它们无法撤消。

你说的是什么意思

所有请求实际上都没有通过

后端已经收到这些请求,处理它们并作出响应。它们无法撤消。

您可以使用它。如果您可以将API URL排列在一个数组中,并按照您希望调用API的顺序排列,那么您可以使用以下方法:

// consider these as API urls
urls = ['url1', 'url2', 'url3']

from(this.urls).pipe(
  concatMap((eachUrl, index) => {
    if (index === 1) {
      return throwError('Error in Second') // simulated error thrown by second API call
    }
    // simulated API calls, you just need to return this, not the error simulation
    // return your API call here and that's all under your concatMap()
    return of(eachUrl).pipe(tap(_ => console.log('API url invoked -> ', eachUrl)))
  })
).subscribe((data) => {
  console.log(`data in subscribe `, data);
}, (err) => {
  console.log(`error in subscribe: `, err)
})
您将看到第三个API调用不会触发

concatMap()将等待请求完成,然后再发送下一个请求

请参见此处的示例:

您可以使用它。如果您可以将API URL排列在一个数组中,并按照您希望调用API的顺序排列,那么您可以使用以下方法:

// consider these as API urls
urls = ['url1', 'url2', 'url3']

from(this.urls).pipe(
  concatMap((eachUrl, index) => {
    if (index === 1) {
      return throwError('Error in Second') // simulated error thrown by second API call
    }
    // simulated API calls, you just need to return this, not the error simulation
    // return your API call here and that's all under your concatMap()
    return of(eachUrl).pipe(tap(_ => console.log('API url invoked -> ', eachUrl)))
  })
).subscribe((data) => {
  console.log(`data in subscribe `, data);
}, (err) => {
  console.log(`error in subscribe: `, err)
})
您将看到第三个API调用不会触发

concatMap()将等待请求完成,然后再发送下一个请求


请看这里的一个例子:

好吧,我想在我订阅之前,请求仍然可以停止。在我的第一个示例中,我认为如果第二个请求(我在
管道(map())
中调用)失败,也许我可以阻止以后订阅第一个请求。或者这毫无意义?你唯一能做的就是按顺序发出请求,这意味着:如果第一个请求成功,你只发送第二个请求。对,我想也许在我订阅之前,请求仍然可以停止。在我的第一个示例中,我认为如果第二个请求(我在
管道(map())
中调用)失败,也许我可以阻止以后订阅第一个请求。或者这毫无意义?你唯一能做的就是按顺序发出请求,这意味着:如果第一个请求成功,你只发送第二个请求。添加的答案没有解决你的问题吗?添加的答案没有解决你的问题吗?
// consider these as API urls
urls = ['url1', 'url2', 'url3']

from(this.urls).pipe(
  concatMap((eachUrl, index) => {
    if (index === 1) {
      return throwError('Error in Second') // simulated error thrown by second API call
    }
    // simulated API calls, you just need to return this, not the error simulation
    // return your API call here and that's all under your concatMap()
    return of(eachUrl).pipe(tap(_ => console.log('API url invoked -> ', eachUrl)))
  })
).subscribe((data) => {
  console.log(`data in subscribe `, data);
}, (err) => {
  console.log(`error in subscribe: `, err)
})