node.jserror-handlingpromiseasync-awaitbluebird

How to handle asynchronous error in Node.js


Is there any alternative to Bluebird's Promise.try function? As I'm using async/await not interested in adding bluebird dependency.

Is there a better way to capture asynchronous error in Node.JS

await Promise.try(async function() {
    // Some code with async operations like readline
    await this.nextCall(batchData, true);
}).catch(async function(err) {
    // My error handling
});

Any inbuilt functions in Node.js 10.x ?

UPDATE :

Is there a better way to capture asynchronous error in Node.JS

try {
    let interfaceVal = lineReader.createInterface({
        input: fs.createReadStream(filePath)
    });
    interfaceVal.on('line', async (line) => {
        throw new Error('My custom Error');
    });
    // some sync operations
} catch(e) {
    // This catch won't get called with custom error
    console.log(e);
}

Any idea to capture such asynchronous errors?


Solution

  • There is no reason to wrap async function with Promise.try. The purpose of Promise.try is to handle both synchronous errors and rejections similarly:

    Start the chain of promises with Promise.try. Any synchronous exceptions will be turned into rejections on the returned promise.

    This is already done with async since it always returns a promise.

    This can be used at top level with async IIFE:

    (async function() {
        await this.nextCall(batchData, true);
    })().catch(console.error);
    

    Or in case async is nested it can be omitted, a rejection can be handled in parent async function with try..catch, as another answer explains.


    In this case an error can be caught only inside async function:

        interfaceVal.on('line', async (line) => {
          try {
            throw new Error('My custom Error');
          } catch (err) {
            console.error(err);
          }
        });
    

    The use of non-promise API (Node stream) doesn't allow for error handling with promises. Stream callback ignores rejected promises and doesn't allow to propagate the error outside the stream.

    Callbacks can be converted to promises only when they are expected to be called once. This is not the case with line. In this case asynchronous iterator can be used, this one of its use cases.

    Event emitter can be converted to async iterator with p-event and iterated with for await of inside async function:

    try {
        let interfaceVal = lineReader.createInterface({
            input: fs.createReadStream(filePath)
        });
    
        const asyncIterator = pEvent.iterator(interfaceVal, 'line', {
          resolutionEvents: ['close']
        });
    
        for await (const event of asyncIterator) {
            console.log('line', event);
            // throw new Error('My custom Error');
        }
    } catch(e) {
        console.log(e);
    }