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()
.the
You are throwing an error in an asynchronous function, instead of rejecting the promise.
Change throw Error("")
to reject("")
:
promiseFunc()
.then(result => {
console.info(`.THEN:: ${result}`)
})
.catch(error => {
console.info(`.CATCH:: ${error}`)
})
function promiseFunc() {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject("setTimeout's callback error")
resolve('resolution')
}, 1000)
})
}
Or add a try-catch statement, and reject it in the catch block
setTimeout(() => {
try {
throw new Error("setTimeout's callback error")
} catch(error) {
reject(error)
}
resolve('resolution')
}, 1000)