Is it possible to catch asynchronous errors using the ES6 .catch
syntax of promises? For example, the following doesn't work (the .catch doesn't catch the error):
new Promise((resolve, reject)=>{
setTimeout(()=>{throw new Error("uh oh")}, 1);
}).then(number=>{
console.log("Number: " + number);
}).catch(e=>{
console.log("Error: " + e);
});
But this synchronous version does:
new Promise((resolve, reject)=>{
throw new Error("uh oh");
}).then(number=>{
console.log("Number: " + number);
}).catch(e=>{
console.log("Error: " + e);
});
Is the only solution to do something like the following, using a try/catch block and reject
ing the error in the catch?
new Promise((resolve, reject)=>{
try {
setTimeout(()=>{throw new Error("uh oh")}, 1);
}
catch(e) {
reject(e);
}
}).then(number=>{
console.log("Number: " + number);
}).catch(e=>{
console.log("Error: " + e);
});
For the sake of this question, assume the part of the code that is throwing the Error is in another named function, so it doesn't have access to the reject
function.
Thanks!!
Edit: Here is a more complete example of what I'd like to do, in JSFiddle.
There isn't a way to catch an error thrown like your first example. The problem here is that you are using the Explicit Promise Construction Antipattern. You are trying to have the
Promise
constructor do more than it needs to do.Instead, you should promisify the smallest amount of asynchronous functionality and build on top of that. In this case, that would involve producing a promise that waits for a certain amount of time before resolving. Most 3rd party promise libraries already have a
.delay()
method, but it's very easy to create your own:Then you can build on top of that, and catch the error easily:
Well next time post your code, because your ability to describe the problem with English will never be as good as actual code. By "asynchronous function" do you mean a function that returns a promise? If so ...
It doesn't matter how deep the errors throw in your Promises. Here's an example function
three
which calls a functiontwo
which calls a functionone
which has potential to throw an Error in the event the JSON is formed poorly. Each step makes a valuable contribution to the final computation, but in the eventone
throw an error, it will bubble up through the entire chain of Promises.Otherwise by "asynchronous function" you mean it's a function that does not return a Promise and maybe uses a continuation instead? In which case, we'll modify
one
to wrap the async function in a promise, thentwo
andthree
will work the same. Of importance, I did not usetry
/catch
in any of my Promise functionsOh, and if you have an a function
f
which does not function in either of these two ways – ie function that throws an error but doesn't return a promise or send the error to the continuation - you're dealing with a piece of rubbish and the code you write to depend onf
will be rubbish too.Use
resolve()
,reject()
withinPromise
constructor. Handle error at eitheronRejected
or.catch()
.Note, once error is handled,
onFulfilled
at chained.then()
, if any, should be reached, unlessthrow
is used withinonRejected
or.catch()
, to explicitly pass error to chained.then(_, onRejected)
or.catch()