0

我正在使用 RxJs concatMap 来控制我的应用程序中某些数据的保存。

this.myService.saveData(this.updatedData)
  .pipe(
    tap(data1Res => this.onData1Success(data1Res)),
    concatMap(() => this.myService.saveOne(this.OneData)),
    tap(data2Res => this.onData2Success(data2Res)),
    concatMap(() => this.myService.saveTwo(this.TwoData)),
    tap(data3Res => this.onData3Success(data3Res)),
    concatMap(() => this.myService.saveThree(this.ThreeData)),
    tap(data4Res => this.onData4Success(data4Res)),
    concatMap(() => this.myService.saveFour(this.FourData)),
  )
  .subscribe(
    res => this.onSaveSuccess(), // Reload values
    err => console.log('error while saving', err) // Save to file or db
  );

目前这有效,但是即使说“this.OneData”为空或为空,它也会执行......我怎样才能让它基本上不发送 NoOpp 请求?

4

2 回答 2

1
this.myService.saveData(this.updatedData)
  .pipe(
    tap(data1Res => this.onData1Success(data1Res)),
    concatMap(() => this.myService.saveOne(this.OneData)),
    filter(res => res !== null),
    tap(data2Res => this.onData2Success(data2Res)),
    ...

  )
  .subscribe(
    res => this.onSaveSuccess(), // Reload values
    err => console.log('error while saving', err) // Save to file or db
  );
于 2020-02-26T17:14:18.763 回答
0

可以像这样使用过滤器来确保它在为空或 null 时不会运行

this.myService.saveData(this.updatedData)
  .pipe(
    tap(data1Res => this.onData1Success(data1Res)),
    filter(res => res),
    concatMap(() => this.myService.saveOne(this.OneData)),
   ...
  )
  .subscribe(
    res => this.onSaveSuccess(), // Reload values
    err => console.log('error while saving', err) // Save to file or db
  );
于 2020-02-27T11:29:02.223 回答