# forEachAsync

> A node- and browser-ready async counterpart of Array.prototype.forEach

Latest version **3.0.0** (published 2014-01-13) · Apache2 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install forEachAsync
pnpm add forEachAsync
yarn add forEachAsync
bun add forEachAsync
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2014-01-13 |
| First published | 2011-07-13 |
| Weekly downloads | 0 |
| License | Apache2 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 20 |
| Author | AJ ONeal |
| Maintainers | coolaj86 |
| Keywords | futuresjs, forEach, for, forEachAsync, async, futures, each |

## Links

- npm: https://www.npmjs.com/package/forEachAsync
- Repository: https://github.com/FuturesJS/forEachAsync
- Issues: https://github.com/FuturesJS/forEachAsync/issues
- npm.io page: https://npm.io/package/forEachAsync

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2014-01-13
- 3.0.0-rc1 (beta) — 2013-08-06
- 2.2.1 — 2014-01-13
- 2.2.0 — 2012-07-03
- 2.1.1 — 2011-07-13

## README

forEachAsync
===

As I do every few years, I decided to rewrite FuturesJS.
This year's remake is extremely lightweight.

v3.x - Diet Cola Edition
(published on npm as beta, so you must use the @3.x - and don't worry, v2.x is still supported)

Analogous to `[].forEach`, but handles items asynchronously with a final callback passed to `then`.

This is the most essential piece of the [`ArrayAsync`](https://github.com/FuturesJS/ArrayAsync) package.

Usage
===

It's as simple as you could guess:

```javascript
  // waits for one request to finish before beginning the next
  forEachAsync(['dogs', 'cats', 'octocats'], function (next, element, index, array) {
    getPics(element, next);
  
  // then after all of the elements have been handled
  // the final callback fires to let you know it's all done
  }).then(function () {
    console.log('All requests have finished');
  });

  // where `getPics` might be an asynchronous web request such as this
  function getPics(animal, cb) {
    var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
    $.getJSON(
      flickerAPI
    , { tags: thing
      , tagmode: "any"
      , format: "json"
      , success: function (data) {
          console.log('teh animals:', data);
        }
      , complete: cb
      }
    );
  }
```


Browser Installation
===

You can install from bower:

```bash
bower install forEachAsync
```

Or download the raw file from <https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js>:

```bash
wget https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js
```

```javascript
(function () {
  'use strict';

  var forEachAsync = window.forEachAsync
    ;

  // do stuff ...
}());
```

Or you can build it alongside other libraries:

```bash
npm install -g pakmanager
npm install forEachAsync --save
pakmanager -e browser build
```

```html
<script src="pakmanaged.js"></script>
```

```javascript
(function () {
  'use strict';

  var forEachAsync = require('forEachAsync').forEachAsync
    ;

  // do stuff ...
}());
```


Node Installation
===

```bash
npm install --save forEachAsync@3.x
```

API
===

**`forEachAsync(array, callback[, thisArg])`**

Parameters

  * `array` Array of elements to iterate over
  * `callback` Function to execute for each element, takes 4 arguments
    * `next` the function to call when the current element has been dealt with
    * `element` a single element of the aforementioned array
    * `index` the index of the current element
    * `array` the same array mentioned above
  * `thisArg` Object to use as `this` when executing `callback`

**`forEachAsync#then(done)`**

Parameters

  * `then` is in the return value of `forEachAsync` and accepts a final `done` callback.
    * `done` called after `forEachAsync` is complete, takes no arguments

Internal API
===

`forEachAsync.__BREAK`

This is used internally for the purposes of the `ArrayAsync` library.

Please don't `break` stuff; use `someAsync` or `everyAsync` instead.

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