Giter Site home page Giter Site logo

adityavs / foreachasync Goto Github PK

View Code? Open in Web Editor NEW

This project forked from futuresjs/foreachasync

0.0 1.0 0.0 284 KB

forEachAsync - browser and node ready

Home Page: http://futuresjs.github.io/forEachAsync/

License: Apache License 2.0

Shell 6.56% JavaScript 93.44%

foreachasync's Introduction

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

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

http://youtu.be/O7egvEz4scA

Usage

  // 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 });
  }
  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:

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

Browser Installation

You can install from bower:

bower install --save [email protected]

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 ...
}());

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

Node Installation

npm install --save [email protected]

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

foreachasync's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.