# async-replace-es6

> Light Weight string.replace working asynchronously

Latest version **1.0.5** (published 2017-12-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install async-replace-es6
pnpm add async-replace-es6
yarn add async-replace-es6
bun add async-replace-es6
```

## 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-12-30 |
| First published | 2017-12-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Daybr3ak |
| Maintainers | daybr3ak |
| Keywords | string, replace, async, asynchronous, promises, es6 |

## Links

- npm: https://www.npmjs.com/package/async-replace-es6
- Repository: https://github.com/Daybr3ak/async-replace-es6
- Homepage: https://github.com/Daybr3ak/async-replace-es6#readme
- Issues: https://github.com/Daybr3ak/async-replace-es6/issues
- npm.io page: https://npm.io/package/async-replace-es6

## 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.5 (latest) — 2017-12-30
- 1.0.4 — 2017-12-30
- 1.0.3 — 2017-12-30
- 1.0.2 — 2017-12-30
- 1.0.1 — 2017-12-30
- 1.0.0 — 2017-12-30

## README

Run replace on a string with an asynchronous function (promises).

## Usage

async-replace-es6 has the same api as `String.prototype.replace`. Only the callback is an async function.

This is usefull if you need the matched part of a string before running an asynchronous operation.

```js
import { replace } from 'async-replace-es6';
const { replace } = require('async-replace-es6');
```

```js
const replaced = await replace(originalString, /someregex/g, asyncReplacer);
```

Or

```js
replace(originalString, /someregex/, asyncReplacer)
    .then(replaced => console.log(replaced));
```

Example of asynchronous replacer :

```js
async function asyncReplace(match, group1, group2, ...) {
    // here match is an url
    const response = await fetch(match);
    const json = await reponse.json();
    return json['title'];
}

function asyncReplacer(match, ...groups) {
    // match is a file path
    return new Promise((resolve, reject) => {
        fs.readFile(match, (err, data) => {
            if (err) {
                return reject(err);
            }
            resolve(data.toString());
        });
    });
}
```

If your promises need to be done Sequentially here is a solution:

```js
const { replaceSeq } = require('async-replace-es6');

replaceSeq(originalString, /someregex/g, asyncReplacer)
    .then(replaced => console.log(replaced));
```

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