You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a use case where I always want to do something when a promise is settled, but with minor differences depending on whether the promise was fulfilled or rejected. However, I don't want to change the fate of the returned promise.
What I'm looking for is a combination of .finally() and .reflect(), where my handler is always called and can observe the state of the promise, but returning a promise with the same eventual state as the promise I invoke on (unless my handler returns a rejected promise, as usual).
My use case is sending a "task completed" notification to a message queue, regardless of whether the task succeeded or failed, but with contents indicating success or failure. However, the returned promise should remain fulfilled or rejected so the caller can naturally proceed depending on the result (without requiring the caller to understand the PromiseInspection object).
There's a few workarounds for this:
functiontheHandler(err,value){// do some stuff}// ElsewherereturnsomePromiseChain().tapCatch(theHandler).tap(value=>theHandler(null,value));
Declaring the function out-of-band is a bit of an awkward maneuver to accomplish this.
Another option:
returnsomePromiseChain().reflect().then(pi=>{// do some stuffreturnpi.isRejected() ? Promise.reject(pi.reason()) : Promise.resolve(pi.value());})
This is a bit verbose... a wrapper function converting a PromiseInspection back into a Promise would help, but still feels like a hack.
I have a use case where I always want to do something when a promise is settled, but with minor differences depending on whether the promise was fulfilled or rejected. However, I don't want to change the fate of the returned promise.
What I'm looking for is a combination of
.finally()
and.reflect()
, where my handler is always called and can observe the state of the promise, but returning a promise with the same eventual state as the promise I invoke on (unless my handler returns a rejected promise, as usual).My use case is sending a "task completed" notification to a message queue, regardless of whether the task succeeded or failed, but with contents indicating success or failure. However, the returned promise should remain fulfilled or rejected so the caller can naturally proceed depending on the result (without requiring the caller to understand the PromiseInspection object).
There's a few workarounds for this:
Declaring the function out-of-band is a bit of an awkward maneuver to accomplish this.
Another option:
This is a bit verbose... a wrapper function converting a PromiseInspection back into a Promise would help, but still feels like a hack.
I'm using this currently:
It seems like this might be useful to others.
The text was updated successfully, but these errors were encountered: