Has anyone used toPromise
and Promise.all
from rxjs in http? Please give me an example.
How can I check both of the request is success in component with promise.all
return some message like API is success?
getPostAsync() {
return this.http.get('https://jsonplaceholder.typicode.com/posts')
.pipe(map((res:Response) => { return res.json();}))
.pipe(catchError((error) => { return throwError(error);}))
}
getPostAsync1() {
return this.http.get('https://jsonplaceholder.typicode.com/posts/1')
.pipe(map((res:Response) => { return res.json();}))
.pipe(catchError((error) => { return throwError(error);}))
}
I wouldn't use toPromise
nor Promise.all
when handling requests. It's better to handle it using observables and operators. I've written an article about replacing Promises with RxJS. Instead of Promise.all
, I'd use forkJoin
to listen for the completion of two or more requests.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments