Reputation: 1179
I 've Error handler function:
function onError(message, source, lineno, colno, error) { sendRequestToSendMail(arguments) }
window.onerror = onError
I have also async tasks with promises and I want catch exceptions in them. I don't need to repeat yourself like:
doSomething1()
.then(doSomething2(), onError)
.then(doSomething3(), onError)
.then(doSomething4(), onError)
How to implement global error handler for all promises (like window.onError) ?
Upvotes: 0
Views: 52
Reputation: 203409
It's not a global error handler (which doesn't sound like a great idea to me tbh), but since errors are propagated through the promise chain, you can shorten your code (and get rid of the repetition) by adding a final .catch()
statement to your chain:
doSomething1()
.then(doSomething2())
.then(doSomething3())
.then(doSomething4())
.catch(onError)
That would catch any rejections thrown by any of the doSomething*
functions.
Upvotes: 1