javascript - How to catch an error on an eventEmitter that runs an async function on 'emit'? - Stack Overflow

I am having some trouble catching errors on async functions that are are run by an eventEmitter on emit

I am having some trouble catching errors on async functions that are are run by an eventEmitter on emit.

Here is the code:

const EventEmitter = require('events')

const eventEmitter = new EventEmitter()
eventEmitter.addListener('callSyncFunction', syncFunction)
eventEmitter.addListener('callAsyncFunction', asyncFunction)

function syncFunction() {
    throw new Error('Sync function error')
}

async function asyncFunction() {
    throw new Error('Async function error')
}

try {
    eventEmitter.emit('callSyncFunction')
} catch(e) {
    console.log(e)
}
// Works and prints 'Sync function error'

try {
    eventEmitter.emit('callAsyncFunction')
} catch(e) {
    console.log(e)
}
// Does not  work and gives an Unhandled promise rejection

I am able to catch errors when a synchronous function is called by the eventEmitter but unable. to catch errors when async functions are run. As suggested on .html#events_capture_rejections_of_promises I tried enabled captureRejections: true but it still does not help capture those errors.

Apart from using a library like emittery, is there any solution to this?

I am having some trouble catching errors on async functions that are are run by an eventEmitter on emit.

Here is the code:

const EventEmitter = require('events')

const eventEmitter = new EventEmitter()
eventEmitter.addListener('callSyncFunction', syncFunction)
eventEmitter.addListener('callAsyncFunction', asyncFunction)

function syncFunction() {
    throw new Error('Sync function error')
}

async function asyncFunction() {
    throw new Error('Async function error')
}

try {
    eventEmitter.emit('callSyncFunction')
} catch(e) {
    console.log(e)
}
// Works and prints 'Sync function error'

try {
    eventEmitter.emit('callAsyncFunction')
} catch(e) {
    console.log(e)
}
// Does not  work and gives an Unhandled promise rejection

I am able to catch errors when a synchronous function is called by the eventEmitter but unable. to catch errors when async functions are run. As suggested on https://nodejs/api/events.html#events_capture_rejections_of_promises I tried enabled captureRejections: true but it still does not help capture those errors.

Apart from using a library like emittery, is there any solution to this?

Share Improve this question asked Oct 28, 2020 at 7:53 philosopherphilosopher 1,1513 gold badges17 silver badges34 bronze badges 1
  • As stated in the documentation, "The handler routes the exception asynchronously to the […] 'error' event handler". It does not make emit() throw an error. Notice that event emitters are one-way streets, you can send events but you won't get anything back. – Bergi Commented Oct 29, 2020 at 1:18
Add a ment  | 

4 Answers 4

Reset to default 4

You can't use nodejs EventEmitter to catch errors from async listeners. The emit method is fully synchronous. Setting capture Rejections: true just allows you to catch async errors through error event. You should use some third-party lib for this, e.g EventEmitter2

Why don't you create a wrapper Listener and resolve the Async function inside it

function WrapperListener(){
    asyncFunction().then(result=>{
            //dosomething
        }).catch(e=>{
           console.log(e)
    })
}

I ended up using this wrapper for my purposes: events-async.

It is easy. Use 'error' event :

GIVEN:

const EventEmitter = require('events')

const eventEmitter = new EventEmitter({ captureRejections: true });
eventEmitter.on('callSyncFunction', ()=>{
    throw new Error('Sync function error')
})
eventEmitter.on('callAsyncFunction', async ()=>{
    throw new Error('Async function error')
})

GIVEN: Error handling

eventEmitter.on('error', (e)=>{
    console.log("Error caught: ", e.message)
})

WHEN

eventEmitter.emit('callSyncFunction', {})
eventEmitter.emit('callAsyncFunction',{})

THEN

Expect console output:

Error caught: Sync function error
Error caught: Async function error

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742283733a4414956.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信