Skip to content Skip to sidebar Skip to footer

How To Handle Error And Use Promises Correctly

To begin with, I am taking a follow up from this question I posted few moments ago Now, I thought I knew Aysnc and Promise but clearly I am missing something. Referencing to the T

Solution 1:

Async await code looks cleaner and easy to read. The Promises were created to solve the callback hell problem but chaining a lot of promises also confusing. So async wait is a syntactical sugar and you can use any one of the .then or async await.

If you are using simple promises syntax then you can use .then.then.then.catch() syntax.

if you are using async and await then you have to use try catch. All the await will go in try and the catch condition would go in single catch.

Both these can be used when API/function you are using returns a promise.

Solution 2:

// So this is how promises work (as you may already be familiar with)functionpromiseFu() {
    returnnewPromise((resolve, reject) => {
        reject();
    })
    .catch(e => {
        // The exception has been handledconsole.error("Error begin in promiseFu()!")
        throw e; // <- now this triggers the second exception
    })
}

// Asynchronous functions are the functions that ALWAYS returns a promise// therefore, this is also correctasyncfunctionasyncFu() {
    returnnewPromise((resolve, reject) => {
        reject();
    })
    .catch(e => {
        // The exception has been handledconsole.error("Error begin in promiseFu()!")
        throw e; // <- now this triggers the second exception
    })
    .catch(e => {
        // Here the second exception gets handled as well, and asyncFu does not throw any exception in the end
    })
}

// Now the power of async awaitasyncfunctionasyncMainFu() {
    // using await before an async function would make it wait for the function to complete asynchronously before moving onawaitasyncFu()

    // await would do the same with promises as well// await promiseFu() // <- this is correct as well// BUT now, if you see promiseFu() is throwing the second exception which is not yet handled,// asyncMainFu() would throw the same exception as well. unless handled by a try..catch blocktry {
        awaitpromiseFu()
    } catch(e) {
        // handling the exception thrown by promiseFu and not throwing any new exception // is a good idea if you think caller of asyncMainFu() might not handle it.
    }
}

Solution 3:

try...catch in async function is syntactic sugar for catch() in raw promise. If raw promises are used for some reason (legacy code) then catch() may be used. This shouldn't be a problem in Node, since recent versions support async..await.

Notice that try..catch catches both synchronous and asynchronous errors in async. This should be taken into account to not leave synchronous errors unhandled with plain promise.

If API doesn't support promises, you cannot expect that promise rejection that is returned from a function be handled by API, so you need to do this yourself.

Post a Comment for "How To Handle Error And Use Promises Correctly"