Skip to content

Async counterparts for each of the Array methods - forEachAsync, everyAsync, someAsync, filterAsync, mapAsync, reduceAsync, reduceRightAsync

License

Notifications You must be signed in to change notification settings

FuturesJS/ArrayAsync

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ArrayAsync

v3.x - Diet Cola Edition

ArrayAsync provides asynchronous counterparts for each of the Array iterate methods

  • forEachAsync
  • everyAsync
  • someAsync
  • filterAsync
  • mapAsync
  • reduceAsync
  • reduceRightAsync* (uses reversed copy of original array)

Each of the other array methods is a thin (12-line long) wrapper around the diet (27-line long) forEachAsync, which is the core workhorse.

Where size is a concern and you don't need all 86 lines of code, I would invite you to simply look at the source and copy/paste what you need (and attribute, of course).

Browser Installation

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

  var filterAsync = window.filterAsync
    ;

  filterAsync(['dogs', 'cats', 'octocats'], function (next, element) {
    askTheUserIfTheyLikeThis(element, function (likesIt) {
      next(likesIt);
    });
  }).then(function (newArr) {
    showTheUserThingsTheyLike(newArr);
  });
}());

Node Installation

npm install array-async
(function () {
  'use strict';

  var ArrayAsync = require('async-array').filterAsync
    ;

  filterAsync(['dogs', 'cats', 'octocats'], function (next, element) {
    askTheUserIfTheyLikeThis(element, function (likesIt) {
      next(likesIt);
    });
  }).then(function (newArr) {
    showTheUserThingsTheyLike(newArr);
  });
}());

Different Names for the Same Thing

I'm not sure which is the clear win yet:

Call each *Async function as needed

  var mapAsync = require('array-async').mapAsync
    ;

Use them attached to ArrayAsync object

  var ArrayAsync = require('array-async').ArrayAsync
    ;

  // Access each without the trailing 'Async'
  ArrayAsync.reduce(arr, fn);

Attach the async methods to Array

  require('array-async').infect(Array);

  Array.filterAsync(array, fn);

I've been the prototype route before... it was a bad idea.

About

Async counterparts for each of the Array methods - forEachAsync, everyAsync, someAsync, filterAsync, mapAsync, reduceAsync, reduceRightAsync

Resources

License

Stars

Watchers

Forks

Packages

No packages published