https://github.com/vmlweb/promise-catcher
Simple promise try catch wrapper written in Typescript.
https://github.com/vmlweb/promise-catcher
Last synced: 9 months ago
JSON representation
Simple promise try catch wrapper written in Typescript.
- Host: GitHub
- URL: https://github.com/vmlweb/promise-catcher
- Owner: Vmlweb
- License: mit
- Created: 2017-01-22T12:33:52.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2017-01-30T17:39:47.000Z (over 9 years ago)
- Last Synced: 2025-10-08T15:58:34.304Z (9 months ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/promise-catcher
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Promise Catcher
Simple promise try catch wrapper written in Typescript. Suggestions are welcome.
## Installation
You can grab the latest version via NPM.
```bash
npm install --save promise-catcher
```
Then use require through Typescript.
```javascript
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.
```javascript
(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.
```javascript
//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()
}
}())
```
```javascript
//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.
```javascript
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()
}))
```
```javascript
//Using throws in jasmine
it('should work', catcher.jasmine(async done => {
if (problem){
throw new Error('message')
}
done()
}))
```
```javascript
//Using throws in jasmine testing angular async
it('should work', async(inject([Service], catcher.angular(async (service: Service) => {
if (problem){
throw new Error('message')
}
}))))
```
```javascript
//Use with any callbacks
catcher.wraps(...)
//Use with any throwing
catcher.throws(...)
```