1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
forEachAsync
===
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.
v3.x - Diet Cola Edition
---
As I do every few years, I decided to rewrite FuturesJS.
This year's remake is extremely lightweight.
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 [`ArrayAsync`](https://github.com/FuturesJS/ArrayAsync)`.someAsync` or [`ArrayAsync`](https://github.com/FuturesJS/ArrayAsync)`.everyAsync` instead.
|