1

I am using RxJs concatMap to control the saving of some data in my application.

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
  );

Currently this works however it also executes even if say "this.OneData" is empty or null...how can I keep it from sending essentially a NoOpp request?

2
  • 1
    you can use filter(r => r !== null ) to filter nulls or anything else Commented Feb 26, 2020 at 16:50
  • Thanks for the response so what would the call look like exactly...concatMap(filter(() => this.myService.saveOne(this.OneData) !== null)) Commented Feb 26, 2020 at 16:54

2 Answers 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
  );
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the response Cenk ...so this will not call "saveOne" if "this.OneData" is Null?
If the saveOne's res is null then the others are not called. Filter breaks the pipe when the value is false.
Okay, what if I want "saveTwo" and " saveFour" to execute because "this.TwoData" and "this.FourData" are populated but I don't want the others to execute because they are not populated...doesn't sound like "filter" will work for that.
Actually this pipe usage is not efficient, I don't understand what you completely do.
It's usage is to call api's in a certain order one after another to avoid nested .subscribes...so for that it's pretty efficient.
0

Filter can be used like this to make sure it doesn't run if it is empty or 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
  );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.