# promised-reducer

> Fold promise queue and tell detect sync or async

Latest version **0.2.0** (published 2017-01-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install promised-reducer
pnpm add promised-reducer
yarn add promised-reducer
bun add promised-reducer
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2017-01-04 |
| First published | 2016-05-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | mizchi |
| Maintainers | mizchi |

## Links

- npm: https://www.npmjs.com/package/promised-reducer
- Repository: https://github.com/mizchi/promised-reducer
- Homepage: https://github.com/mizchi/promised-reducer#readme
- Issues: https://github.com/mizchi/promised-reducer/issues
- npm.io page: https://npm.io/package/promised-reducer

## Recent versions

- 0.2.0 (latest) — 2017-01-04
- 0.1.0 — 2016-05-28

## README

# PromisedReducer

Fold promise queue and tell detect sync or async

```
npm install promised-reducer
```

## Example

```js
import PromisedReducer from "promised-reducer";

const reducer = new PromisedReducer({count: 0});

reducer.on(":process-updating", () => {
  // called by each queue
});

reducer.on(":process-async-updating", () => {
  // called by each async queue (including synced update)
});

reducer.on(":start-async-updating", () => {
  // called once on start async
});

reducer.on(":end-anync-updating", () => {
  // called once on end async
});

reducer.on(":update", state => {
  // emit on non-promise update or end-async-update
  console.log(state):
});

reducer.update(({count}) => ({count: count + 1}))
// => logging {count: 1}
reducer.update(({count}) => Promise.resolve({count: count + 1}))
reducer.update(({count}) => Promise.resolve({count: count + 1}))
// => logging {count: 3} not 2: queuing promises are reduced to one result.
```

## Middlewares

Middleware function type is `<T>(t: T | Promise<T>) => T | Promise<T>`;
Handle Promise.resolve if you need to consider promise by upper middleware chain.

```js
const logger = (state_or_promise) => {
  return Promise.resolve(state_or_promise)
  .then(state => {
    console.log("log:", state);
    return state;
  })
}

const reducer = new PromisedReducer({count: 0}, [logger]);
```

## With Rx

PromisedReducer extends EventEmitter. Handle it as EventEmitter.

```js
const reducer = new PromisedReducer({count: 0});
const updateStream: Rx.Observable<{count: number;}> = Rx.Observable.formEvent(reducer, ":update")
```

## LICENSE

MIT

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