A wrapper for promise.catch() that guarantees a resolved promise
normally if you want to use 'await' in the root of your project, you have to wrap it thusly:
let promise=Promise.reject(new Error('test error'))
try{
;(async ()=>{
try{
let result = await promise
console.log(`The promise results in : ${result}`)
}catch(error){
console.log(`The promise responded with an error: ${error.message}`)
}
})()
}catch(error){
console.log(`some error has occurred: $(error.message)`)
process.exit(-1)
}
This seems very akward and not very readable. That is why I made Catch
:
let promise=Promise.reject(new Error('test error'))
Catch((async ()=>{
let result = await Catch(promise, Catch.log.message.prefix(`The promise responded with an error:`))
if (result) console.log(`The promise results in : ${result}`)
})(),Catch.exit.withLog)
Not really -- it hides the errors by default and lets you specify an error handler in a way that results in more readable code. Very frequently, we discard caught errors, because it just denotes that a resource or operation did not succeed. This is also very frequently evident from the fact that the result is undefined or null, and often we need no more than that.
It is for this purpose this utility was made. It just abstracts away the requirements of dealing with errors when there is no interest in the errors that result from a promise.