Currently, I'm trying to decide what pattern I should use while dealing with errors inside a Promise. For instance, I have the code below
promiseFunc()
.then(result => {
console.info(`.THEN:: ${result}`)
})
.catch(error => {
console.info(`.CATCH:: ${error}`)
})
function promiseFunc() {
return new Promise((resolve, reject) => {
setTimeout(() => {
throw Error("setTimeout's callback error")
resolve('resolution')
}, 1000)
})
}
What I can't get is what approach should be used to reject the Promise if a function inside it (setTimeout(), in my case) throws an Error. In other words, I need a rejection instead of an error, but the only idea that comes to my mind is to add a try/catch block and reject the Promise from the catch.