|
|
@ -5,45 +5,45 @@ Analogous to `[].forEach`, but handles items asynchronously with a final callbac |
|
|
|
|
|
|
|
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. |
|
|
|
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. |
|
|
|
|
|
|
|
Usage |
|
|
|
=== |
|
|
|
----- |
|
|
|
|
|
|
|
```javascript |
|
|
|
// EXAMPLE ASYNC FUNCTION |
|
|
|
|
|
|
|
It's as simple as you could guess: |
|
|
|
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 |
|
|
|
// 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 |
|
|
|
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'); |
|
|
|
}); |
|
|
|
|
|
|
|
// 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 |
|
|
|
} |
|
|
|
); |
|
|
|
} |
|
|
|
``` |
|
|
|
|
|
|
|
### 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 |
|
|
|
=== |
|
|
@ -51,7 +51,7 @@ Browser Installation |
|
|
|
You can install from bower: |
|
|
|
|
|
|
|
```bash |
|
|
|
bower install forEachAsync |
|
|
|
bower install --save forEachAsync@5.x |
|
|
|
``` |
|
|
|
|
|
|
|
Or download the raw file from <https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js>: |
|
|
@ -71,35 +71,13 @@ wget https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js |
|
|
|
}()); |
|
|
|
``` |
|
|
|
|
|
|
|
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 ... |
|
|
|
}()); |
|
|
|
``` |
|
|
|
|
|
|
|
**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@3.x |
|
|
|
npm install --save forEachAsync@5.x |
|
|
|
``` |
|
|
|
|
|
|
|
API |
|
|
@ -111,7 +89,6 @@ 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 |
|
|
|