Skip to content

Commit

Permalink
feat(async): clulib.async.forEach + clulib.async.forEachRight
Browse files Browse the repository at this point in the history
  • Loading branch information
b-strauss committed Feb 12, 2017
1 parent a60dbd1 commit a9fc7df
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/async/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
goog.provide('clulib.async');

/**
* @param {Array<T>} array
* @param {function(T, number, Array<T>): Promise} action
* @returns {Promise}
* @template T
*/
clulib.async.forEach = function (array, action) {
return array.reduce((promise, element, index, arr) => {
return promise.then(() => action(element, index, arr));
}, Promise.resolve());
};

/**
* @param {Array<T>} array
* @param {function(T, number, Array<T>): Promise} action
* @returns {Promise}
* @template T
*/
clulib.async.forEachRight = function (array, action) {
return clulib.async.forEach(array.reverse(), action);
};

0 comments on commit a9fc7df

Please sign in to comment.