admin管理员组文章数量:1331388
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 makeemit()
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
4 Answers
Reset to default 4You 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
版权声明:本文标题:javascript - How to catch an error on an eventEmitter that runs an async function on 'emit'? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742283732a2446562.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论