New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

express-dot-async

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-dot-async

Use Promises with Express like a boss!

latest
Source
npmnpm
Version
0.2.1
Version published
Maintainers
1
Created
Source

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)

Keywords

express

FAQs

Package last updated on 31 May 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts