How can I know if Observable has finalized with error or without error?
up vote
2
down vote
favorite
I need to execute some code when Observable is completed depending on whether has finalized with error or without. I have this code:
const obs = getMyObservable().pipe(finalize(() =>
//here
));
As you see, I'm using finalize operator, but I can't know if has finalized with or without error. Is there some kind of doOnComplete or doOnError operators in rxjs?
javascript rxjs reactive-streams
add a comment |
up vote
2
down vote
favorite
I need to execute some code when Observable is completed depending on whether has finalized with error or without. I have this code:
const obs = getMyObservable().pipe(finalize(() =>
//here
));
As you see, I'm using finalize operator, but I can't know if has finalized with or without error. Is there some kind of doOnComplete or doOnError operators in rxjs?
javascript rxjs reactive-streams
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I need to execute some code when Observable is completed depending on whether has finalized with error or without. I have this code:
const obs = getMyObservable().pipe(finalize(() =>
//here
));
As you see, I'm using finalize operator, but I can't know if has finalized with or without error. Is there some kind of doOnComplete or doOnError operators in rxjs?
javascript rxjs reactive-streams
I need to execute some code when Observable is completed depending on whether has finalized with error or without. I have this code:
const obs = getMyObservable().pipe(finalize(() =>
//here
));
As you see, I'm using finalize operator, but I can't know if has finalized with or without error. Is there some kind of doOnComplete or doOnError operators in rxjs?
javascript rxjs reactive-streams
javascript rxjs reactive-streams
asked May 18 at 10:48
Héctor
9,0891358124
9,0891358124
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
According to https://www.learnrxjs.io/
With the latest RXJS you can use this 3 operators
const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
tap(() => console.log('Action performed before any other')),
catchError(() => console.error('Error emitted'); return of(); ), // We return instead
finalize(() => console.log('Action to be executed always')) // Either Error or Success
);
obs.subscribe(data => console.log(data)); // After everything, we log the output.
Hope it helps
EDIT based on JoniJnm comment
To be More specific, there are, 3 main Pipes:
- Pipes that does alter the result before subscription.
- Pipes that does not alter the result before subscription.
- Special Pipes.
Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.
Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.
Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.
CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).
You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.
If this pipe is not launched, the result is considered without error.
This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
– JoniJnm
Oct 29 at 17:28
1
The order of execution isObservable -> Pipes in order (except finalize) -> subscribe -> finalize Pipewith this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
– Ignacio Bustos
Nov 11 at 16:02
So something like this: stackblitz.com/edit/typescript-kpusau
– JoniJnm
Nov 22 at 8:54
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
According to https://www.learnrxjs.io/
With the latest RXJS you can use this 3 operators
const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
tap(() => console.log('Action performed before any other')),
catchError(() => console.error('Error emitted'); return of(); ), // We return instead
finalize(() => console.log('Action to be executed always')) // Either Error or Success
);
obs.subscribe(data => console.log(data)); // After everything, we log the output.
Hope it helps
EDIT based on JoniJnm comment
To be More specific, there are, 3 main Pipes:
- Pipes that does alter the result before subscription.
- Pipes that does not alter the result before subscription.
- Special Pipes.
Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.
Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.
Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.
CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).
You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.
If this pipe is not launched, the result is considered without error.
This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
– JoniJnm
Oct 29 at 17:28
1
The order of execution isObservable -> Pipes in order (except finalize) -> subscribe -> finalize Pipewith this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
– Ignacio Bustos
Nov 11 at 16:02
So something like this: stackblitz.com/edit/typescript-kpusau
– JoniJnm
Nov 22 at 8:54
add a comment |
up vote
1
down vote
accepted
According to https://www.learnrxjs.io/
With the latest RXJS you can use this 3 operators
const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
tap(() => console.log('Action performed before any other')),
catchError(() => console.error('Error emitted'); return of(); ), // We return instead
finalize(() => console.log('Action to be executed always')) // Either Error or Success
);
obs.subscribe(data => console.log(data)); // After everything, we log the output.
Hope it helps
EDIT based on JoniJnm comment
To be More specific, there are, 3 main Pipes:
- Pipes that does alter the result before subscription.
- Pipes that does not alter the result before subscription.
- Special Pipes.
Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.
Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.
Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.
CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).
You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.
If this pipe is not launched, the result is considered without error.
This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
– JoniJnm
Oct 29 at 17:28
1
The order of execution isObservable -> Pipes in order (except finalize) -> subscribe -> finalize Pipewith this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
– Ignacio Bustos
Nov 11 at 16:02
So something like this: stackblitz.com/edit/typescript-kpusau
– JoniJnm
Nov 22 at 8:54
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
According to https://www.learnrxjs.io/
With the latest RXJS you can use this 3 operators
const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
tap(() => console.log('Action performed before any other')),
catchError(() => console.error('Error emitted'); return of(); ), // We return instead
finalize(() => console.log('Action to be executed always')) // Either Error or Success
);
obs.subscribe(data => console.log(data)); // After everything, we log the output.
Hope it helps
EDIT based on JoniJnm comment
To be More specific, there are, 3 main Pipes:
- Pipes that does alter the result before subscription.
- Pipes that does not alter the result before subscription.
- Special Pipes.
Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.
Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.
Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.
CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).
You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.
If this pipe is not launched, the result is considered without error.
According to https://www.learnrxjs.io/
With the latest RXJS you can use this 3 operators
const obs = getMyObservable().pipe( // Let's assume 'obs' returns an array
tap(() => console.log('Action performed before any other')),
catchError(() => console.error('Error emitted'); return of(); ), // We return instead
finalize(() => console.log('Action to be executed always')) // Either Error or Success
);
obs.subscribe(data => console.log(data)); // After everything, we log the output.
Hope it helps
EDIT based on JoniJnm comment
To be More specific, there are, 3 main Pipes:
- Pipes that does alter the result before subscription.
- Pipes that does not alter the result before subscription.
- Special Pipes.
Tap for example is from the second type, it can takes the input from the observable or previous pipes and do anything with it but cannot change the result of the pipe for the next step.
Map is similar but it belongs to the first type of Pipe, it takes an input and has to return an output that can be used in the next pipe or final subscription.
Finalise is a special pipe, it does the same than Tap but after subscription, it is good for example to log final results or to cancel subscription after it completes.
CatchError is a pipe which alters the result but it is only called if the previous step has thrown an error. This is used to avoid unhandled error and you should return an observable "default" instead of the failed observable (so we handle the error and the app does not break).
You can guess when your observable had an Error if catchError has been launched and handle it straight away before it reach the subscription.
If this pipe is not launched, the result is considered without error.
edited Nov 11 at 16:17
answered Jun 27 at 17:52
Ignacio Bustos
4792719
4792719
This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
– JoniJnm
Oct 29 at 17:28
1
The order of execution isObservable -> Pipes in order (except finalize) -> subscribe -> finalize Pipewith this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
– Ignacio Bustos
Nov 11 at 16:02
So something like this: stackblitz.com/edit/typescript-kpusau
– JoniJnm
Nov 22 at 8:54
add a comment |
This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
– JoniJnm
Oct 29 at 17:28
1
The order of execution isObservable -> Pipes in order (except finalize) -> subscribe -> finalize Pipewith this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.
– Ignacio Bustos
Nov 11 at 16:02
So something like this: stackblitz.com/edit/typescript-kpusau
– JoniJnm
Nov 22 at 8:54
This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
– JoniJnm
Oct 29 at 17:28
This doesn't solve the problem. A function (in a pipe) that is executed after everything but knowing if there was an error or not.
– JoniJnm
Oct 29 at 17:28
1
1
The order of execution is
Observable -> Pipes in order (except finalize) -> subscribe -> finalize Pipe with this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.– Ignacio Bustos
Nov 11 at 16:02
The order of execution is
Observable -> Pipes in order (except finalize) -> subscribe -> finalize Pipe with this. A subscription gets what catchError() has done if there was an error. I will explain more in the answer.– Ignacio Bustos
Nov 11 at 16:02
So something like this: stackblitz.com/edit/typescript-kpusau
– JoniJnm
Nov 22 at 8:54
So something like this: stackblitz.com/edit/typescript-kpusau
– JoniJnm
Nov 22 at 8:54
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50409658%2fhow-can-i-know-if-observable-has-finalized-with-error-or-without-error%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown