A specific sequence needs to be executed in the following steps:
- read a small piece of data
- clear the data storage
- re-save the small piece of data from step 1
Each operation has its own method that returns an Observable
. The sequence suggested is as follows:
this.storage.get('key').pipe(
switchMap((x) => this.storage.clear().pipe(map(()=>x))),
switchMap((x) => this.set('key', x)
);
The repetitive use of switchMap()
seems cumbersome. Although the sequence works, it might benefit from simplification.
An attempt was made using concat()
at first, but it emitted values individually. Additionally, merge
could not be utilized as it would run the observables simultaneously and we need to ensure that the .clear()
function does not execute concurrently with either .get()
or .set()
.
Is there an appropriate operator or creation function that can achieve the following:
- initiate the first Observable
- wait until the first Observable starts before triggering the second one
- then return an array or map containing both results
Alternatively, is there a simpler way to handle the double call to switchMap()
?