# foreachasync

> A node- and browser-ready async (now with promises) counterpart of Array.prototype.forEach

Latest version **5.1.3** (published 2019-04-05) · (MIT OR Apache-2.0) license · 0 weekly downloads

## Install

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

## 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 | 5.1.3 |
| Published | 2019-04-05 |
| First published | 2014-01-22 |
| Weekly downloads | 0 |
| License | (MIT OR Apache-2.0) |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 16.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | AJ ONeal |
| Maintainers | coolaj86 |
| Keywords | futuresjs, forEach, for, forEachAsync, async, futures, promise, promises, each |

## Links

- npm: https://www.npmjs.com/package/foreachasync
- Repository: https://git.coolaj86.com/coolaj86/foreachasync.js
- Issues: https://git.coolaj86.com/coolaj86/foreachasync.js/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

- 5.1.3 (latest) — 2019-04-05
- 5.1.2 — 2018-03-28
- 5.0.5 — 2015-01-07
- 5.0.4 — 2015-01-06
- 3.0.0 — 2014-01-22

## README

forEachAsync.js
===

| A [Root](https://rootprojects.org) project

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.

For cases where you want to loop through batches of items at once (as opposed to strictly one-by-one as forEachAsync does), check out [`forAllAsync`](https://github.com/FuturesJS/forAllAsync) and [`lateral`](https://github.com/FuturesJS/lateral).

For cases where you want to loop through all items at once and we able to know when they're all done see [`join`](https://github.com/FuturesJS/join)

v5.x
----

We jumped from 3.x to 5.x because I'm considering creating a backwards-and-forwards compatible 4.x that
uses AngularJS-style function introspection to allow for having the next param.
Straight up, that's probably a bad idea and waste of time so I hope I don't actually do it.

Screencast
---

<https://youtu.be/O7egvEz4scA>

Usage
-----

```javascript
  // EXAMPLE ASYNC FUNCTION

  function getPicsAsync(animal) {
    var flickerApi = "http://api.flickr.com/services/feeds/photos_public.gne?tagmode=any&format=json&tags=" + animal;

    return requestAsync({ url: flickerApi });
  }
```

```javascript
  forEachAsync(['dogs', 'cats', 'octocats'], function (element) {
    return getPicsAsync(element);
  }).then(function () {
    // then after all of the elements have been handled
    // the final callback fires to let you know it's all done
    console.log('All requests have finished');
  });
```

### Supplying your own Promises Implementation

If native ES6 promises are not available, then you should supply your own Promises/A+
implementation like so:

```javascript
  forEachAsync = forEachAsync.create(window.Promise || require('bluebird'));
```

Browser Installation
===

You can install from bower:

```bash
bower install --save forEachAsync@5.x
```

Or download the raw file from <https://git.coolaj86.com/coolaj86/foreachasync.js/raw/branch/master/foreachasync.js>:

```bash
wget https://git.coolaj86.com/coolaj86/foreachasync.js/raw/branch/master/foreachasync.js
```

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

  var forEachAsync = window.forEachAsync;

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

**Note**: If you need both 3.x/4.x and 5.x version of `forEachAsync` in the browser... good luck with that...

Node Installation
===

```bash
npm install --save foreachasync@5.x
```

API
===

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

Parameters

  * `array` Array of elements to iterate over
  * `callback` Function to execute for each element, takes 4 arguments
    * `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 [`ArrayAsync`](https://github.com/FuturesJS/ArrayAsync)`.someAsync` or [`ArrayAsync`](https://github.com/FuturesJS/ArrayAsync)`.everyAsync` instead.

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