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.org/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?