I have a list of observables that needs to invoke APIs synchronously. The APIs fails for asynchronous operation. Hence an observable needs to wait for the previous observable to complete before executing, I have written a recursive solution as shown below, is there a better way to do it.
private myMethod(params) { let paramsCopy = params.slice(); let param = paramsCopy.shift(); if (param) { let resource: ResourceModel= param.resource; return resource.doPost(JSON.stringify(param.data)) .pipe(mergeMap(res => {return myMethod(paramsCopy)}) ,catchError((err) => handleError())); } else { return //completed actions; } }
switchMapoperator. Take a look link.concatMap - waits for the previous Observable to complete before creating the next one