Compare commits

..

No commits in common. "master" and "v5.0.0" have entirely different histories.

6 changed files with 62 additions and 66 deletions

View File

@ -1,8 +1,6 @@
forEachAsync.js forEachAsync
=== ===
| A [Root](https://rootprojects.org) project
Analogous to `[].forEach`, but handles items asynchronously with a final callback passed to `then`. Analogous to `[].forEach`, but handles items asynchronously with a final callback passed to `then`.
This is the most essential piece of the [`ArrayAsync`](https://github.com/FuturesJS/ArrayAsync) package. This is the most essential piece of the [`ArrayAsync`](https://github.com/FuturesJS/ArrayAsync) package.
@ -21,7 +19,7 @@ Straight up, that's probably a bad idea and waste of time so I hope I don't actu
Screencast Screencast
--- ---
<https://youtu.be/O7egvEz4scA> <http://youtu.be/O7egvEz4scA>
Usage Usage
----- -----
@ -64,17 +62,18 @@ You can install from bower:
bower install --save forEachAsync@5.x bower install --save forEachAsync@5.x
``` ```
Or download the raw file from <https://git.coolaj86.com/coolaj86/foreachasync.js/raw/branch/master/foreachasync.js>: Or download the raw file from <https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js>:
```bash ```bash
wget https://git.coolaj86.com/coolaj86/foreachasync.js/raw/branch/master/foreachasync.js wget https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js
``` ```
```javascript ```javascript
(function () { (function () {
'use strict'; 'use strict';
var forEachAsync = window.forEachAsync; var forEachAsync = window.forEachAsync
;
// do stuff ... // do stuff ...
}()); }());
@ -86,7 +85,7 @@ Node Installation
=== ===
```bash ```bash
npm install --save foreachasync@5.x npm install --save forEachAsync@5.x
``` ```
API API

View File

@ -2,15 +2,17 @@
;(function (exports) { ;(function (exports) {
'use strict'; 'use strict';
var BREAK = {}; var BREAK = {}
var exp = {}; , exp = {}
;
function create(PromiseA) { function create(PromiseA) {
PromiseA = PromiseA.Promise || PromiseA; PromiseA = PromiseA.Promise || PromiseA;
function forEachAsync(arr, fn, thisArg) { function forEachAsync(arr, fn, thisArg) {
var result = PromiseA.resolve(); var result = PromiseA.resolve()
;
arr.forEach(function (item, k) { arr.forEach(function (item, k) {
result = result.then(function () { result = result.then(function () {
@ -24,11 +26,11 @@
ret = result = fn(item, k, arr); ret = result = fn(item, k, arr);
} }
if (!ret || !ret.then) { if (!ret.then) {
ret = PromiseA.resolve(ret); ret = PromiseA.resolve(result);
} }
return ret.then(function (val) { ret.then(function (val) {
if (val === forEachAsync.__BREAK) { if (val === forEachAsync.__BREAK) {
return PromiseA.reject(new Error('break')); return PromiseA.reject(new Error('break'));
//throw new Error('break'); //throw new Error('break');
@ -59,15 +61,24 @@
exports.create = forEachAsync.create = function () {}; exports.create = forEachAsync.create = function () {};
*/ */
/* globals Promise */
if ('undefined' !== typeof Promise) { try {
exp.forEachAsync = create(Promise); exp.forEachAsync = create(require('bluebird'));
} } catch(e) {
else { if ('undefined' !== typeof PromiseA) {
try { exp.forEachAsync = create(Promise);
exp.forEachAsync = create(require('bluebird')); } else {
} catch(e) { try {
console.warn("This version of node doesn't support promises. Please `npm install --save bluebird` in your project."); exp.forEachAsync = create(require('es6-promise'));
} catch(e) {
try {
exp.forEachAsync = create(require('rsvp'));
} catch(e) {
console.warning('forEachAsync needs requires a promise implementation and your environment does not provide one.'
+ '\nYou may provide your own by calling forEachAsync.create(Promise) with a PromiseA+ implementation'
);
}
}
} }
} }
@ -76,4 +87,4 @@
}; };
exports.forEachAsync.create = create; exports.forEachAsync.create = create;
}('undefined' !== typeof exports && exports || window)); }('undefined' !== typeof exports && exports || new Function('return this')()));

5
package-lock.json generated
View File

@ -1,5 +0,0 @@
{
"name": "foreachasync",
"version": "5.1.3",
"lockfileVersion": 1
}

View File

@ -1,10 +1,9 @@
{ {
"name": "foreachasync", "name": "foreachasync",
"version": "5.1.3", "version": "5.0.0",
"description": "A node- and browser-ready async (now with promises) counterpart of Array.prototype.forEach", "description": "A node- and browser-ready async (now with promises) counterpart of Array.prototype.forEach",
"homepage": "https://git.coolaj86.com/coolaj86/foreachasync.js", "homepage": "https://github.com/FuturesJS/forEachAsync",
"main": "foreachasync.js", "main": "forEachAsync.js",
"files": [],
"directories": { "directories": {
"test": "test" "test": "test"
}, },
@ -13,7 +12,7 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://git.coolaj86.com/coolaj86/foreachasync.js.git" "url": "git://github.com/FuturesJS/forEachAsync.git"
}, },
"keywords": [ "keywords": [
"futuresjs", "futuresjs",
@ -26,13 +25,14 @@
"promises", "promises",
"each" "each"
], ],
"trulyOptionalDependencies": { "optionalDependencies": {
"bluebird": "^3.5.1" "bluebird": "^2.5.3"
}, },
"author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)", "author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.com/)",
"license": "(MIT OR Apache-2.0)", "license": "Apache2",
"bugs": { "bugs": {
"url": "https://git.coolaj86.com/coolaj86/foreachasync.js/issues" "url": "https://github.com/FuturesJS/forEachAsync/issues"
}, },
"dependencies": {} "dependencies": {
}
} }

View File

@ -1,32 +1,27 @@
(function () { (function () {
"use strict"; "use strict";
var PromiseA = require('bluebird'); var PromiseA = require('bluebird')
var forEachAsync = require('./forEachAsync').forEachAsync; , forEachAsync = require('./forEachAsync').forEachAsync
var context = {}; , context = {}
;
forEachAsync([0, 500, 70, 200, 400, 100], function (element, i, arr) { forEachAsync([0, 500, 70, 200, 400, 100], function (element, i, arr) {
console.log(i, '/', arr.length, 'began'); // test that array order is as expected
console.log(element, 'is element', i, 'of', arr.length);
var result;
// test that thisness is applied // test that thisness is applied
this[element] = i; this[element] = i;
if (i % 2) { if (i > 2) {
// test that synchronous callbacks don't mess things up // test that synchronous callbacks don't mess things up
result = PromiseA.resolve(); return PromiseA.resolve();
} else { } else {
// test asynchronous callbacks // test asynchronous callbacks
result = new PromiseA(function (resolve/*, reject*/) { return new Promise(function (resolve/*, reject*/) {
setTimeout(resolve, element); setTimeout(resolve, element);
}); });
} }
return result.then(function () {
// test that array order is as expected
console.log(i, '/', arr.length, 'complete');
});
}, context).then(function () { }, context).then(function () {
// test that thisness carried // test that thisness carried
console.log('context', context); console.log('context', context);

View File

@ -1,30 +1,26 @@
(function () { (function () {
"use strict"; "use strict";
/* globals Promise */ var forEachAsync = require('./forEachAsync').forEachAsync.create(Promise)
var forEachAsync = require('./forEachAsync').forEachAsync.create(Promise); , context = {}
var context = {}; ;
forEachAsync([0, 500, 70, 200, 400, 100], function (element, i, arr) { forEachAsync([0, 500, 70, 200, 400, 100], function (element, i, arr) {
var p; // test that array order is as expected
console.log(element, 'is element', i, 'of', arr.length);
// test that thisness is applied // test that thisness is applied
this[element] = i; this[element] = i;
if (i % 2) { if (i > 2) {
// test that synchronous callbacks don't mess things up // test that synchronous callbacks don't mess things up
p = Promise.resolve(); return Promise.resolve();
} else { } else {
// test asynchronous callbacks // test asynchronous callbacks
p = new Promise(function (resolve/*, reject*/) { return new Promise(function (resolve/*, reject*/) {
setTimeout(resolve, element); setTimeout(resolve, element);
}); });
} }
return p.then(function () {
// test that array order is as expected
console.log(element, 'is element', i, 'of', arr.length);
});
}, context).then(function () { }, context).then(function () {
// test that thisness carried // test that thisness carried
console.log('context', context); console.log('context', context);