Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

forEachAsync

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

forEachAsync

A node- and browser-ready async counterpart of Array.prototype.forEach

  • 3.0.0-rc1
  • beta
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.7K
decreased by-62.23%
Maintainers
1
Weekly downloads
 
Created
Source

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:

  // an asynchronous web request
  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
      }
    );
  }

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

Browser Installation

You can download and include forEachAsync.js:

<script src="https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js"></script>
(function () {
  'use strict';

  var forEachAsync = window.forEachAsync
    ;

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

Or you can build it alongside other libraries:

npm install -g pakmanager
npm install forEachAsync@3.x --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 someAsync or everyAsync instead.

Keywords

FAQs

Package last updated on 06 Aug 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc