From 1a4ba1c41e4963b7f1d205f1a747aba484549935 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Tue, 6 Aug 2013 02:51:22 -0700 Subject: [PATCH] added a test and fixed some bugs. yay! --- forEachAsync.js | 7 ++++--- test.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 test.js diff --git a/forEachAsync.js b/forEachAsync.js index 3ebf7e8..abb8834 100644 --- a/forEachAsync.js +++ b/forEachAsync.js @@ -8,15 +8,16 @@ ; function next(BREAK, newArr) { - if (0 === arr.length || BREAK === forEachAsync.__BREAK) { + index += 1; + + if (index === arr.length || BREAK === forEachAsync.__BREAK) { dones.forEach(function (done) { done.call(thisArg, newArr); }); return; } - index += 1; - fn.call(thisArg, next, arr.shift(), index, arr); + fn.call(thisArg, next, arr[index], index, arr); } setTimeout(next, 4); diff --git a/test.js b/test.js new file mode 100644 index 0000000..c881fcd --- /dev/null +++ b/test.js @@ -0,0 +1,15 @@ +(function () { + "use strict"; + + var forEachAsync = require('../forEachAsync').forEachAsync + ; + + forEachAsync([0, 500, 70, 800], function (next, element, i, arr) { + console.log(element, 'is element', i, 'of', arr.length); + this[element] = i; + setTimeout(next, element); + }, {}).then(function () { + console.log(this); + }); + +}());