Answer the question
In order to leave comments, you need to log in
How to execute 2 requests in a row in rxjs and process the results of only the first one?
There are 2 requests for the backend. To execute the second query, you need the result of the first query.
After executing both requests, you need to execute the function, into which you need to pass again the results of the first request.
Now it looks something like this:
this.profileDocumentService.getDataOne(profileId, identityTranslationCreateContract).pipe(
tap((someDataFromOne) => {
if (someDataFromOne.document) {
this.documentApiAdapter.getDataTwo(translationResponse.id as number, someDataFromOne.document).subscribe();
}
}),
map((someDataFromOne) => this.mapIdentityTranslationDocument([someDataFromOne]))
Answer the question
In order to leave comments, you need to log in
In general, I solved the problem with this design. Maybe it will help someone:
this.profileDocumentService.getDataOne(profileId, identityTranslationCreateContract).pipe(
switchMap((previousFioResponse) => {
if (previousFioResponse.document) {
return this.documentApiAdapter
.getDataTwo(previousFioResponse.id, previousFioDocument.document)
.pipe(map(() => this.mapPreviousFioDocument([previousFioResponse])));
}
return of(this.mapPreviousFioDocument([previousFioDocument]));
})
);
In order not to clutter up the code with callbacks, there are promises, use them.
If only .subscribe() confuses you, do
await firstValueFrom(this.documentApiAdapter.getDataTwo(translationResponse.id as number, someDataFromOne.document)) and don't rack your brains.
rxjs7+. Self signing, self unsubscribing.
https://rxjs.dev/api/index/function/firstValueFrom
As it was correctly said above - about callbacks, for example, and a car of checks on if-s - look towards exchaustMap - I use it myself - in NgRx effects - when you need to wait for the result from the back and then do something
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question