forEachAsync - browser and node ready
Go to file
AJ ONeal a60f4c3058 added reference to forAllAsync, lateral, and join 2014-01-22 16:19:21 -07:00
examples added build script 2014-01-22 13:43:49 -07:00
.gitignore ignore bower_components 2014-01-22 12:46:07 -07:00
LICENSE let's not forget the all-powerful license 2013-08-06 02:53:28 -07:00
README.md added reference to forAllAsync, lateral, and join 2014-01-22 16:19:21 -07:00
bower.json updated metadata 2014-01-13 16:07:10 -07:00
forEachAsync.js added leading semicolon 2014-01-13 16:06:43 -07:00
package.json name is now lowercase 2014-01-22 12:46:54 -07:00
test.js comment about all the things that are being tested 2013-08-06 03:00:37 -07:00

README.md

forEachAsync

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

This is the most essential piece of the 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 and 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

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:

  // 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:

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
(function () {
  'use strict';

  var forEachAsync = window.forEachAsync
    ;

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

Or you can build it alongside other libraries:

npm install -g pakmanager
npm install forEachAsync --save
pakmanager -e browser build
<script src="pakmanaged.js"></script>
(function () {
  'use strict';

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

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

Node Installation

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.someAsync or ArrayAsync.everyAsync instead.