# through2-asyncmap

> An async version of through2-map -- async Array.prototype.map for streams

Latest version **1.1.0** (published 2014-06-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install through2-asyncmap
pnpm add through2-asyncmap
yarn add through2-asyncmap
bun add through2-asyncmap
```

## 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.1.0 |
| Published | 2014-06-10 |
| First published | 2014-06-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Bryce B. Baril |
| Maintainers | bryce |
| Keywords | streams, through, through2, async, map |

## Links

- npm: https://www.npmjs.com/package/through2-asyncmap
- Repository: https://github.com/brycebaril/through2-asyncmap
- Issues: https://github.com/brycebaril/through2-asyncmap/issues
- npm.io page: https://npm.io/package/through2-asyncmap

## Dependencies (2)

- [xtend](https://npm.io/package/xtend.md) ^3.0.0
- [through2](https://npm.io/package/through2.md) ~0.5.1

## Alternatives

- [byte-size](https://npm.io/package/byte-size.md) — 2.1M weekly downloads
- [speed-limiter](https://npm.io/package/speed-limiter.md) — 16.0K weekly downloads
- [@powersync/node](https://npm.io/package/@powersync/node.md) — 10.9K weekly downloads
- [@ledgerhq/coin-cardano](https://npm.io/package/@ledgerhq/coin-cardano.md) — 1.0K weekly downloads
- [@jayesol/jayeson.lib.streamfinder](https://npm.io/package/@jayesol/jayeson.lib.streamfinder.md) — 1.0K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2014-06-10
- 1.0.1 — 2014-06-10
- 1.0.0 — 2014-06-10

## README

through2-asyncmap
============

[![NPM](https://nodei.co/npm/through2-asyncmap.png)](https://nodei.co/npm/through2-asyncmap/)

An async-only version of [through2-map](http://npm.im/through2-map)

```js

var map = require("through2-asyncmap")

var truncate = map(function (chunk, callback) {
  setImmediate(function () {
    callback(null, chunk.slice(0, 10))
  })
})

// vs. with through2:
var truncate = through2(function (chunk, encoding, callback) {
  setImmediate(function () {
    this.push(chunk.slice(0, 10))
    return callback()
  })
})

// Then use your map:
source.pipe(truncate).pipe(sink)

// Additionally accepts `wantStrings` argument to convert buffers into strings
var stripTags = map({wantStrings: true}, function (str, next) {
  // OMG don't actually use this
  setImmediate(function () {next(null, str.replace(/<.*?>/g, ""))}
})

// Works like `Array.prototype.map` meaning you can specify a function that
// takes up to two* arguments: fn(chunk, index)
var spaceout = map({wantStrings: true}, function (chunk, index, callback) {
  setImmediate(functio () {
    callback(null, (index % 2 == 0) ? chunk + "\n\n" : chunk)
  })
})

// vs. with through2:
var spaceout = through2(function (chunk, encoding, callback) {
  if (this.index == undefined) {
    this.index = 0
  }
  setImmediate(function () {
    var buf = (this.index++ % 2 == 0) ? Buffer.concat(chunk, new Buffer("\n\n")) : chunk
    this.push(buf)
    return callback()
  })
})

```

*Differences from `Array.prototype.map`:
  * Cannot insert `null` elements into the stream without aborting.
  * No third `array` callback argument. That would require realizing the entire stream, which is generally counter-productive to stream operations.
  * `Array.prototype.map` doesn't modify the source Array, which is somewhat nonsensical when applied to streams.

API
---

`require("through2-asyncmap")([options,] fn)`
---

Create a `stream.Transform` instance that will call `fn(chunk, [index,] callback)` on each stream segment.

`callback` is expected to return two arguments: `callback(error, replacementChunk)` and will replace the provided chunk with `replacementChunk`

If `error` is true, it will emit that error on the stream.

`var Tx = require("through2-asyncmap").ctor([options,] fn)`
---

Create a reusable `stream.Transform` TYPE that can be called via `new Tx` or `Tx()` to create an instance.

`require("through2-asyncmap").obj([options,] fn)`
---

Create a `through2-asyncmap` instance that defaults to `objectMode: true`.

`require("through2-asyncmap").objCtor([options,] fn)`
---

Just like ctor, but with `objectMode: true` defaulting to true.

Options
-------

  * wantStrings: Automatically call chunk.toString() for the super lazy.
  * all other through2 options

LICENSE
=======

MIT

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