0.1.1 • Published 7 years ago

mware-async v0.1.1

Weekly downloads
1
License
BSD-3-Clause
Repository
github
Last release
7 years ago

mware-async

Build Status Coverage Status

mware-async is an extension to mware but for an async/await style middleware stack.

Usage

import mware from 'mware-async';
const { use, run } = mware();

const context = {};

// add middleware
use(async (ctx, next) => {
    console.assert(ctx === context);
    console.log('a');
    await next();
    console.log('b');
});

use(async (ctx, next) => {
    console.log('c');
    await next();
});

// run stack
run(context).then(() => console.log('fin')); // a, c, b, fin

Errors

// error middleware
use(async () => throw new Error('Bad stuff!'));

// run
run().catch(err => console.error(err)); // [Error: Bad stuff!]

Installation

NPM

npm install --save mware-async

Yarn

yarn add mware-async

API

mware()

Returns a mware instance.

Instance

#use(fn...)
  • fn: Function|[]Function, Async middleware functions to add to stack.
#run([...args])
  • args: *, Arguments to pass to each middleware function.

Returns a promise.

License

BSD-3-Clause

Copyright (c) 2016 9Technology