promise-catcher
v0.4.2
Published
Simple promise try catch wrapper written in Typescript.
Maintainers
Readme
Promise Catcher
Simple promise try catch wrapper written in Typescript. Suggestions are welcome.
Installation
You can grab the latest version via NPM.
npm install --save promise-catcherThen use require through Typescript.
import * as catcher from 'promise-catcher'The Problem
When using ES6 promises with packages that do not catch your async rejections you may be familiar with this error.
(node:) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id:): RequestError:To solve this we need to wrap our calls in try catch statements to make sure we are handling them correctly.
//Using callbacks in express
app.get('/url', () => {
return async (req, res, next) => {
try{
if (problem){
throw new Error('message')
}
}catch(err){
next(err)
return
}
next()
}
}())//Using throws in jasmine
describe('Test', () => {
it('test', () => {
return async done => {
try{
if (problem){
throw new Error('message')
}
}catch(err){
throw err
}
done()
}
}())
})Our Solution
This package contains convenience functions that wrap your async functions in try catch and pass the error onwards properly.
import * as catcher from 'promise-catcher'
//Using callbacks in express
app.get('/url', catcher.express(async (req, res, next) => {
if (problem){
throw new Error('message')
}
next()
}))//Using throws in jasmine
describe('Test', catcher.jasmine(async done => {
if (problem){
throw new Error('message')
}
done()
}))//Use with any callbacks
catcher.wraps(...)
//Use with any throwing
catcher.throws(...)