[RxJS 6] The Catch and Rethrow RxJs Error Handling Strategy and the finalize Operator

Sometime we want to set a default or fallback value when network request failed.

http$
    .pipe(
        map(res => Object.values['payload']),
        shareReplay(),
        catchError(err => of([])) // return an empty value
);

Sometime we want to just throw the error again:

http$
    .pipe(
        catchError(err => {
            return throwError(err)
        }), // put catchError and finalize to make sure they will be only run once
        finalize(() => console.log('Finalize executed...'))
        map(res => Object.values['payload']),
        shareReplay()
);        

猜你喜欢

转载自www.cnblogs.com/Answer1215/p/9279008.html