# esnext-async

> Patterns for asynchronous computation in ES.next

Latest version **0.0.9** (published 2016-09-24) · MIT license · 0 weekly downloads

## Install

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

## 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.0.9 |
| Published | 2016-09-24 |
| First published | 2016-03-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Vinson Chuong |
| Maintainers | vinsonchuong |
| Keywords | esnext, async, observable, promise |

## Links

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

## Dependencies (2)

- [babel-runtime](https://npm.io/package/babel-runtime.md) ^6.5
- [zen-observable](https://npm.io/package/zen-observable.md) ^0.3.0

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

- 0.0.9 (latest) — 2016-09-24
- 0.0.8 — 2016-09-04
- 0.0.7 — 2016-08-29
- 0.0.6 — 2016-06-30
- 0.0.5 — 2016-06-27
- 0.0.4 — 2016-06-27
- 0.0.3 — 2016-06-15
- 0.0.2 — 2016-06-14
- 0.0.1 — 2016-03-14

## README

# esnext-async
[![Build Status](https://travis-ci.org/vinsonchuong/esnext-async.svg?branch=master)](https://travis-ci.org/vinsonchuong/esnext-async)

Patterns for asynchronous iteration in ES.next

## Installing
`esnext-async` is available as an
[npm package](https://www.npmjs.com/package/esnext-async).

## Usage

### Entry Points

#### Run
```js
import {run} from 'esnext-async';

run(async () => {
  const foo = await fs.readFile('foo');
  await fs.writeFile('bar', foo);
});
```

Execute an `async` function, re-throwing any exceptions and making them fatal.

#### Loop
```js
import {loop} from 'esnext-async';

loop(async () => {
  const value = await observable;
  console.log(value);
});
```

Execute an `async` function over and over, re-throwing any exceptions and
making them fatal. `loop` waits until an "iteration" is complete before
executing the next iteration.

#### TryCatch
```js
import {tryCatch} from 'esnext-async';

app.get('/', tryCatch(async (request, response) => {
  const data = await database.read();
  res.send(data);
}));
```

Wrap an `async` callback function and re-throw any exceptions. `tryCatch` is
useful when passing `async` callback functions to libraries that are unaware
of `async` functions.

### Control Flow

#### Parallel
```js
import {parallel} from 'esnext-async';

test(async (t) => {
  await parallel(
    async () => {
      await browser.open('https://google.com');
    },
    async () => {
      t.is(await browser.requestLogs, 'https://google.com');
      t.is(await browser.requestLogs, 'https://google.com/some-image.png');
    }
  )
});
```

Start multiple sequences of `async` work at the same time and wait until every
sequence is complete. `parallel` is useful for interleaving fast `async` calls
with slow `async` calls. In the above example, the two assertions are executed
after `browser.open` begins but before it resolves.

#### Sleep
```js
import {sleep, run} from 'esnext-async';
run(async () => {
  await sleep();
  await sleep(1000);
});
```

Pauses execution using `setTimeout`. When given no arguments, `0` is passed to
`setTimeout`.

### Observables

#### Observable
```js
import {Observable} from 'esnext-async';

const observable = new Observable((observer) => {
  setTimeout(() => {
    observer.next(1);
    setTimeout(() => {
      observer.next(2);
    }, 0);
  }, 0);
});

observable.forEach((value) => {
  console.log(value);
});
```

An implementation of the
[es-observable](https://github.com/zenparsing/es-observable) specification.

#### AwaitableObservable
```js
import {AwaitableObservable} from 'esnext-async';

const observable = new AwaitableObservable((observer) => {
  setTimeout(() => {
    observer.next(1);
    setTimeout(() => {
      observer.next(2);
    }, 0);
  }, 0);
});

const value1 = await observable;
const value2 = await observable;
```

An observable that behaves like a promise that can be resolved multiple times.
The `AwaitableObservable` provides more control over when to process values by
eliminating callbacks.

## Development
### Getting Started
The application requires the following external dependencies:
* Node.js

The rest of the dependencies are handled through:
```bash
npm install
```

Run tests with:
```bash
npm test
```

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