It might be helpful to consider how you would accomplish this with the synchronous analogue.
Your requirement appears to be that you want your function to communicate the following possible results:
1. succeed with a value
2. fail with an error (if the cause is an RPC error)
3. succeed and/or fail (if the cause is not an RPC error)
The crux of it is path 3. You seem to want more than one "result". If that’s the case, you’ll probably have to bundle your result in an envelope, or emit ancillary information on a side-channel.
Once you resolve the type of the p1 result in such a way that it can convey all of the information you want, the solution will pour from your fingers.
Consider this interpretation:
1. succeed with a {value, error: null}
2. succeed with {value: null, error}
3. succeed with {value, error}
return promise.then(function (value) {
}, function (error) {
if (error.rpc) {
return {value: null, error: error};
} else {
var value = handleInternally();
return {value: value, error: error};
}
});
Or this interpretation:
1. succeed with a value
2. fail with an error (for RPC errors only)
3. succeed or fail depending on the result of the error handler in all other cases, but leave a note that it had to try really hard
return promise.catch(function (error) {
if (error.rpc) {
throw error;
} else {
notify(error);
return retry();
}
});
Also, since you mentioned RPC, I get one free pass to point you in the direction Q-Connection[1]. It’s like RPC, but uses promises as a message passing proxy through which you can pipeline to get around common chatty protocol / latency problems with RPC.
Kris Kowal