Skip to content

Repository files navigation

Throwback

NPM versionNPM downloadsBuild statusTest coverage

Simple asynchronous middleware pattern.

Installation

npm install throwback --save

Usage

Compose asynchronous (promise-returning) functions.

const{ compose }=require("throwback");constfn=compose([asyncfunction(ctx,next){console.log(1);try{awaitnext();}catch(err){console.log("throwback",err);}console.log(4);},asyncfunction(ctx,next){console.log(2);returnnext();}]);// Callback runs at the end of the stack, before// the middleware bubbles back to the beginning.fn({},function(ctx){console.log(3);ctx.status=404;});

Tip: In development (NODE_ENV !== "production"), compose will throw errors when you do something unexpected. In production, the faster non-error code paths are used.

Example

Build a micro HTTP server!

const{ createServer }=require("http");constfinalhandler=require("finalhandler");// Example only, not compatible with single `ctx` arg.const{ compose }=require("throwback");constapp=compose([function({ req, res },next){res.end("Hello world!");}]);createServer(function(req,res){returnapp({ req, res },finalhandler());}).listen(3000);

Use Cases

Inspiration

Built for servie and inspired by koa-compose.

License

MIT

About

An asynchronous middleware pattern

Topics

Resources

Security policy

Stars

27 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages