# sequencialize

> Run async functions in sequence. Queue async functions.

Latest version **1.0.7** (published 2019-11-23) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

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

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.7 |
| Published | 2019-11-23 |
| First published | 2019-11-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 4.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Juji |
| Maintainers | jujiyangasli |
| Keywords | async queue, promise queue, async sequence, promise sequence, async, promise |

## Links

- npm: https://www.npmjs.com/package/sequencialize
- Repository: git@github.com:juji/sequencialize
- npm.io page: https://npm.io/package/sequencialize

## 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

- 1.0.7 (latest) — 2019-11-23
- 1.0.6 — 2019-11-23
- 1.0.5 — 2019-11-23
- 1.0.4 — 2019-11-23
- 1.0.3 — 2019-11-23
- 1.0.2 — 2019-11-23
- 1.0.1 — 2019-11-23
- 1.0.0 — 2019-11-23

## README

# sequencialize

Run async functions in sequence. Queue async functions.

How to make parallel-running async function run sequencially?
This module tries to resolve the problem when something like `findAndUpdate` creates two entries when run almost parallel.

So, we just chain them.

If you are using message broker to queue potentially-breaking parallel function calls, you won't need this.

## Usage
```js
const Sequencer = require('sequencialize')
const sequencer = new Sequencer()

await sequencer.run(func:Function, [namespace:String])
```

It will return the value returned from the original function. So:
```js
const n = 3
const func = async n => n

for(let i = n;i;i--){
  sequencer
    .run(() => func(i))
    .then(v => console.log(v))
}

// 3
// 2
// 1

```

It will run functions in sequence:
```js
const wait = t => new Promise(r => setTimeout(r,t))
const n = 3
const func = n => {
  return wait(1000+ Math.random() * 5000)
    .then(() => console.log(n))
}

for(let i = n;i;i--){
  sequencer.run(() => func(i))
}

// 3
// 2
// 1

```

you can use namespace. Each namespace is it's own sequence:
```js

const wait = t => new Promise(r => setTimeout(r,t))
const n = 3
const func = (namespace, n) => {
  return wait(1000+ Math.random() * 5000)
    .then(() => console.log(`${namespace}: ${n}`))
}

for(let i = n;i;i--){
  sequencer.run(() => func('asdf', i), 'asdf')
  sequencer.run(() => func('ASDF', i), 'ASDF')
}

// each function in a namespace runs in sequence
// asdf: 3
// ASDF: 3
// asdf: 2
// asdf: 1
// ASDF: 2
// ASDF: 1

```

## Use Case

instead of this:
```js
// potentially racing
const handleRequest = () => {

  db.longCalculationAndUpsert({
    where: { user_id },
    data
  })

}
```

write this:
```js

// run in sequence for every user_id
const handleRequest = () => {

  sequencer.run(() => {
    return db.longCalculationAndUpsert({
      where: { user_id },
      data
    })
  }), user_id)

}
```

Cheers,
[juji](https://jujiyangasli.com)

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