PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, November 15, 2022

[FIXED] How to handle asynchronous error in Node.js

 November 15, 2022     async-await, bluebird, error-handling, node.js, promise     No comments   

Issue

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);
}


Answered By - Estus Flask
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing