# async-writer

> The async-writer module makes it possible to asynchronously write data to an output stream while still flushing out bytes in the correct order

Latest version **2.1.3** (published 2016-12-14) · Apache-2.0 license · 0 weekly downloads

## Install

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

## Health

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

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

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.3 |
| Published | 2016-12-14 |
| First published | 2014-09-19 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | separate (@types/async-writer) |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Patrick Steele-Idem |
| Maintainers | mlrawlings, philidem, pnidem |

## Links

- npm: https://www.npmjs.com/package/async-writer
- Repository: https://github.com/marko-js/async-writer
- Homepage: https://github.com/marko-js/async-writer#readme
- Issues: https://github.com/marko-js/async-writer/issues
- npm.io page: https://npm.io/package/async-writer

## Dependencies (2)

- [events](https://npm.io/package/events.md) ^1.0.2
- [complain](https://npm.io/package/complain.md) ^1.0.0

## Recent versions

- 2.1.3 (latest) — 2016-12-14
- 3.0.1 — 2016-10-28
- 3.0.0 — 2016-10-26
- 2.1.2 — 2016-10-18
- 1.4.5 — 2016-10-15
- 2.1.1 — 2016-10-13
- 2.1.0 — 2016-10-13
- 2.0.1 — 2016-10-13
- 2.0.0 — 2016-10-13
- 1.4.4 — 2016-08-31
- 1.4.3 — 2016-08-31
- 1.4.2 — 2016-05-26
- 1.4.1 — 2015-07-24
- 1.4.0 — 2015-05-27
- 1.3.9 — 2015-02-24
- … 16 more at https://npm.io/package/async-writer/versions

## README

async-writer
============

The `async-writer` module makes it possible to asynchronously write to an output stream while still flushing out bytes in the correct order. That is, the `async-writer` module allows you to write parts of a stream out of order and the `async-writer` module does the hard work of ensuring that the output bytes are flushed in the correct order. Content that is written before it is ready to be flushed is buffered and immediately flushed as soon it is ready.

An async writer is helpful if during writing you have to wait for an asynchronous operation to complete before writing to part of a stream. As an example, you might start writing a page to produce HTML and then get to a part of the page that depends on data that has not yet been loaded. With that use case, you can continue to write the remainder of the page and still pipe out the stream to the response. The async writer will ensure that the bytes are flushed out in the correct order.

# Installation

```
npm install async-writer --save
```

# Usage

The simplest usage of an async writer is shown below:

```javascript
var through = require('through');

var output = '';
var stream = through(function write(data) {
        output += data;
    });

var out = require('async-writer').create(stream)
    .on('error', function(err) {
        // Something went wrong during writing
    })
    .on('end', function() {
        // Value of output: "ABC"
    });

out.write('A');
out.write('B');
out.write('C');
out.end();
```

Asynchronous, out-of-order writing to an output stream is shown below:

```javascript
var through = require('through');

var output = '';
var stream = through(function write(data) {
        output += data;
    });

var out = require('async-writer').create(stream)
    .on('error', function(err) {
        // Something went wrong during writing
    })
    .on('end', function() {
        // Value of output: "ABC"
    });

out.write('A');

var asyncOut = out.beginAsync();
setTimeout(function() {
    asyncOut.write('B');
    asyncOut.end();
}, 1000);

out.write('C');
out.end();
```

You can also pipe another stream to a async writer. For example, the following code illustrates how multiple templates could be written to the same async writer:

```javascript
var through = require('through');

var output = '';
var stream = through(function write(data) {
        output += data;
    });

var out = require('async-writer').create(stream)
    .on('error', function(err) {
        // Something went wrong during writing
    })
    .on('end', function() {
        // Value of output: "ABC"
    });

out.write('A');

var asyncOut = out.beginAsync();
require('fs').createReadStream('b.txt', 'utf8')
    .pipe(asyncOut);

out.write('C');
out.end();
```

# Debug mode

Just replace `require('async-writer')` with `require('async-writer/debug')` and you'll get debug output and diagrams of the current state.

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