1

I've been reading about the use of async/await instead of the classic .then() in Promises, but there's still something that I don't understand.

I know that you can replace the classic .then().catch() with async/await and try and catch but that will handle exceptions and rejects like they're the same.

Example:

async function func1() { return new Promise((resolve, reject) => { //throw new Error("error"); reject("rejected"); }); } async function func2() { try { const message = await func1(); console.log(message); } catch(e) { console.log(e); } } func2(); 

Both reject and throw new Error will fall into the catch block but Promises also has the next sintaxis:

then(funcResolve, funcReject).catch(funcCatch) but async/await treats both reject and errors as the same.

Is there any way to differentiate with async/await an error from a reject?

4
  • You have a few options. One is to wrap every asynchronous operation in your async/await with it's own try/catch and possibly modify your error there. You could make your asynchronous functions potentially throw custom errors. If the async operations are network requests, the errors produced there will potentially have properties that non-network errors have, so you can possibly check for that Commented Dec 13, 2019 at 16:36
  • 2
    .catch(func) is just a shortcut for .then(undefined, func). Promises themselves don't distinguish between "rejected because it called the reject function" and "rejected because of an exception" Commented Dec 13, 2019 at 16:36
  • then(funcResolve, funcReject).catch(funcCatch) -- I believe that in that code, funcCatch would ONLY catch errors thrown in funcReject, not errors thrown by the promise or errors thrown in funcResolve Commented Dec 13, 2019 at 16:38
  • (edit) ACTUALLY I'm wrong. promise.then(funcResolve, funcReject).catch(funcCatch) behaves differently than I expect. A rejection from promise goes to funcReject, while an error thrown in funcResolve skips funcReject and goes straight to funcCatch. At least in Chrome Commented Dec 13, 2019 at 16:43

1 Answer 1

1

Based on a small amount of experimentation in Chrome, it looks like promise().then(funcResolve, funcReject).catch(funcCatch) is nearly functionally equivalent to

async function run() { try { try { const result = await promise(); } catch(e) { // funcReject code here } // funcResolve code here } catch(e) { // funcCatch code here // this catches errors generated in funcReject and funcResolve } } 

The only difference is, in the promise version, funcResolve doesn't run after funcReject but in my async code it does.

You can still achieve the same sorts of results if you absolutely need to distinguish between errors from async operations and error from synchronous operations. You can make sure your async operations return a custom class that extends Error, for example, and check for the class in your catch

class AsyncError extends Error{} // in your async code, make sure you transform all errors into AsyncErrors try { promise() } catch(e) { if (e instanceof AsyncError) {} else {} } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.