# throwback

> Simple asynchronous middleware pattern

Latest version **4.1.0** (published 2019-06-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install throwback
pnpm add throwback
yarn add throwback
bun add throwback
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 4.1.0 |
| Published | 2019-06-09 |
| First published | 2016-05-06 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 26.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 27 |
| Author | Blake Embrey |
| Maintainers | blakeembrey |
| Keywords | middleware, async, compose, promise, ware, layer |

## Links

- npm: https://www.npmjs.com/package/throwback
- Repository: https://github.com/serviejs/throwback
- Issues: https://github.com/serviejs/throwback/issues
- npm.io page: https://npm.io/package/throwback

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 4.1.0 (latest) — 2019-06-09
- 4.0.0 — 2018-10-21
- 3.0.0 — 2018-05-21
- 2.0.1 — 2017-05-08
- 2.0.0 — 2017-01-18
- 1.1.1 — 2016-06-20
- 1.1.0 — 2016-05-11
- 1.0.3 — 2016-05-10
- 1.0.2 — 2016-05-07
- 1.0.1 — 2016-05-06

## README

# Throwback

[![NPM version](https://img.shields.io/npm/v/throwback.svg?style=flat)](https://npmjs.org/package/throwback)
[![NPM downloads](https://img.shields.io/npm/dm/throwback.svg?style=flat)](https://npmjs.org/package/throwback)
[![Build status](https://img.shields.io/travis/serviejs/throwback.svg?style=flat)](https://travis-ci.org/serviejs/throwback)
[![Test coverage](https://img.shields.io/coveralls/serviejs/throwback.svg?style=flat)](https://coveralls.io/r/serviejs/throwback?branch=master)

> Simple asynchronous middleware pattern.

## Installation

```
npm install throwback --save
```

## Usage

Compose asynchronous (promise-returning) functions.

```js
const { compose } = require("throwback");

const fn = compose([
  async function(ctx, next) {
    console.log(1);

    try {
      await next();
    } catch (err) {
      console.log("throwback", err);
    }

    console.log(4);
  },
  async function(ctx, next) {
    console.log(2);

    return next();
  }
]);

// 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!

```js
const { createServer } = require("http");
const finalhandler = require("finalhandler"); // Example only, not compatible with single `ctx` arg.
const { compose } = require("throwback");

const app = compose([
  function({ req, res }, next) {
    res.end("Hello world!");
  }
]);

createServer(function(req, res) {
  return app({ req, res }, finalhandler());
}).listen(3000);
```

## Use Cases

- HTTP requests (e.g. [`popsicle`](https://github.com/serviejs/popsicle))
- HTTP servers (e.g. [`servie`](https://github.com/serviejs/servie))
- Processing pipelines (e.g. [`scrappy`](https://github.com/blakeembrey/node-scrappy))

## Inspiration

Built for [`servie`](https://github.com/serviejs) and inspired by [`koa-compose`](https://github.com/koajs/compose).

## License

MIT

---
_Source: https://npm.io/package/throwback · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
