# async-single

> run an async function but only once at a time.

Latest version **1.0.5** (published 2017-11-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-single
pnpm add async-single
yarn add async-single
bun add async-single
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2017-11-07 |
| First published | 2017-08-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | 'Dominic Tarr' |
| Maintainers | dominictarr |

## Links

- npm: https://www.npmjs.com/package/async-single
- Repository: https://github.com/dominictarr/async-single
- Issues: https://github.com/dominictarr/async-single/issues
- npm.io page: https://npm.io/package/async-single

## Recent versions

- 1.0.5 (latest) — 2017-11-07
- 1.0.4 — 2017-10-27
- 1.0.3 — 2017-10-25
- 1.0.2 — 2017-10-01
- 1.0.1 — 2017-08-26
- 1.0.0 — 2017-08-08

## README

# async-single

run an async function but only once at a time.

also, debounce the function with timers.

## example

``` js
var AsyncSingle = requrie('async-single')

var as = AsyncSingle(function (value, cb) {
  fs.writeFile(filename, value, cb) //or some other async thing!
})

//write as often as you like.
//not every write will happen,
//but it will write every so often
as.write('hello world')

as.write('again!')

//when you close, it will finish off any writes!
as.close(function () {...})
```

exploration of async programming style,
by solving the same problem the same way several times.

The problem, is to take an async function,
and wrap it to that it is only called once at a time.

``` js
var async = Single(function (value, cb) {
  ThisMayTakeSomeTime(function () {
    //...
    cb()
  })
})

async.onDrain = function () {
  //called when there is no longer something waiting to write
}

async.write(value)
```

if you call many times quickly, it may write the first time,
but then wait until that succeeds before writing the latest value.
If you have three quick writes
`async.write(1);async.write(2);async.write(3);`
then the first write is fired, then if the async operation
is still in flight before the others are received, the value
of the last write will be performed.

(in practice, we would probably want to delay the first
write so that quick writes get bundled! but we are keeping
this simple as an exercise!)

## License

MIT

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