0.2.1 • Published 6 years ago

express-dot-async v0.2.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

express-dot-async

Have you always wanted to use Express with Promises like a boss?

Basics

Usually you have to do things like this:

app.get('/api/songs', (req, res) => {
  database.find('songs')
    .then(songs => res.json(songs))
    .catch(e => res.status(500).send(e.message))
})

But with express-dot-async you can do it this way:

app.async.get('/api/songs', () => database.find('songs'))

Error handling

express-dot-async contains special HttpError which enable this kind of stuff:

import { HttpError } from 'express-dot-async'

function doStuff(opts) {
  if (!opts.mandatory) {
    throw new HttpError({status: 400, message: 'Mandatory is mandatory!'})
  }
}

Full HD working example

import express from 'express'
import asyncify, { HttpError } from 'express-dot-async'

const app = express()
app.async = asyncify(app)

const getData = (error) => {
  if (error) {
    throw new HttpError({status: 400, message: 'help ducks'})
  }
  return new Promise(resolve => resolve(['ducks', 'dogs', 'cats']))
}

app.async.get('/api/songs', (req) => getData(req.query.error === 'true'))

app.listen(6505)
0.2.1

6 years ago

0.2.0

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago