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
package.
Usage
It's as simple as you could guess:
// waits for one request to finish before beginning the next ; // where `getPics` might be an asynchronous web request such as this { var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?"; $; }
Browser Installation
You can install from bower:
bower install forEachAsync
Or download the raw file from https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js:
wget https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js
{ 'use strict'; var forEachAsync = windowforEachAsync ; // do stuff ...};
Or you can build it alongside other libraries:
npm install -g pakmanagernpm install forEachAsync --savepakmanager -e browser build
{ 'use strict'; var forEachAsync = forEachAsync ; // do stuff ...};
Node Installation
npm install --save forEachAsync@3.x
API
forEachAsync(array, callback[, thisArg])
Parameters
array
Array of elements to iterate overcallback
Function to execute for each element, takes 4 argumentsnext
the function to call when the current element has been dealt withelement
a single element of the aforementioned arrayindex
the index of the current elementarray
the same array mentioned above
thisArg
Object to use asthis
when executingcallback
forEachAsync#then(done)
Parameters
then
is in the return value offorEachAsync
and accepts a finaldone
callback.done
called afterforEachAsync
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.