Compare commits

..

15 Commits

Author SHA1 Message Date
AJ ONeal e534d3a4b3 v5.1.3: remove deps, update link 2019-04-05 12:53:27 -06:00
AJ ONeal 7ba1195ac5 update README.md 2018-03-28 00:24:32 -06:00
AJ ONeal 34d8d7bbc5 v5.1.2 2018-03-27 23:52:32 -06:00
AJ ONeal 7fa166802a update URLs 2018-03-27 23:52:12 -06:00
AJ ONeal 0022ba5abe v5.1.1 2018-03-27 23:50:55 -06:00
AJ ONeal 2f6f9d5e8e update URLs 2018-03-27 23:50:34 -06:00
AJ ONeal 812950328d v5.1.0 2018-03-27 23:41:41 -06:00
AJ ONeal ba81917d4f version bump 2015-01-07 00:55:24 -07:00
AJ ONeal c8875d8d40 promise correct result 2015-01-07 00:55:18 -07:00
AJ ONeal f761f9215c fix #1 check for null / undefined result 2015-01-07 00:53:45 -07:00
AJ ONeal 7218a53487 version bump 2015-01-05 23:22:06 -07:00
AJ ONeal 5689274eb8 console.warning -> console.warn 2015-01-05 23:20:57 -07:00
AJ ONeal 676159ba62 typo fix for native Promise implementation 2015-01-05 23:20:29 -07:00
AJ ONeal 70eda0ad55 fixed order bug 2015-01-02 18:39:27 -07:00
AJ ONeal 8daeae0e1d v5.x 2015-01-02 18:10:44 -07:00
6 changed files with 66 additions and 62 deletions

View File

@ -1,6 +1,8 @@
forEachAsync forEachAsync.js
=== ===
| 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.
@ -19,7 +21,7 @@ Straight up, that's probably a bad idea and waste of time so I hope I don't actu
Screencast Screencast
--- ---
<http://youtu.be/O7egvEz4scA> <https://youtu.be/O7egvEz4scA>
Usage Usage
----- -----
@ -62,18 +64,17 @@ You can install from bower:
bower install --save forEachAsync@5.x bower install --save forEachAsync@5.x
``` ```
Or download the raw file from <https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js>: Or download the raw file from <https://git.coolaj86.com/coolaj86/foreachasync.js/raw/branch/master/foreachasync.js>:
```bash ```bash
wget https://raw.github.com/FuturesJS/forEachAsync/master/forEachAsync.js wget https://git.coolaj86.com/coolaj86/foreachasync.js/raw/branch/master/foreachasync.js
``` ```
```javascript ```javascript
(function () { (function () {
'use strict'; 'use strict';
var forEachAsync = window.forEachAsync var forEachAsync = window.forEachAsync;
;
// do stuff ... // do stuff ...
}()); }());
@ -85,7 +86,7 @@ Node Installation
=== ===
```bash ```bash
npm install --save forEachAsync@5.x npm install --save foreachasync@5.x
``` ```
API API

View File

@ -2,17 +2,15 @@
;(function (exports) { ;(function (exports) {
'use strict'; 'use strict';
var BREAK = {} var BREAK = {};
, exp = {} var 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 () {
@ -26,11 +24,11 @@
ret = result = fn(item, k, arr); ret = result = fn(item, k, arr);
} }
if (!ret.then) { if (!ret || !ret.then) {
ret = PromiseA.resolve(result); ret = PromiseA.resolve(ret);
} }
ret.then(function (val) { return 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');
@ -61,24 +59,15 @@
exports.create = forEachAsync.create = function () {}; exports.create = forEachAsync.create = function () {};
*/ */
/* globals Promise */
try { if ('undefined' !== typeof Promise) {
exp.forEachAsync = create(require('bluebird')); exp.forEachAsync = create(Promise);
} catch(e) { }
if ('undefined' !== typeof PromiseA) { else {
exp.forEachAsync = create(Promise); try {
} else { exp.forEachAsync = create(require('bluebird'));
try { } catch(e) {
exp.forEachAsync = create(require('es6-promise')); console.warn("This version of node doesn't support promises. Please `npm install --save bluebird` in your project.");
} 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'
);
}
}
} }
} }
@ -87,4 +76,4 @@
}; };
exports.forEachAsync.create = create; exports.forEachAsync.create = create;
}('undefined' !== typeof exports && exports || new Function('return this')())); }('undefined' !== typeof exports && exports || window));

5
package-lock.json generated Normal file
View File

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

View File

@ -1,9 +1,10 @@
{ {
"name": "foreachasync", "name": "foreachasync",
"version": "5.0.0", "version": "5.1.3",
"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://github.com/FuturesJS/forEachAsync", "homepage": "https://git.coolaj86.com/coolaj86/foreachasync.js",
"main": "forEachAsync.js", "main": "foreachasync.js",
"files": [],
"directories": { "directories": {
"test": "test" "test": "test"
}, },
@ -12,7 +13,7 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/FuturesJS/forEachAsync.git" "url": "https://git.coolaj86.com/coolaj86/foreachasync.js.git"
}, },
"keywords": [ "keywords": [
"futuresjs", "futuresjs",
@ -25,14 +26,13 @@
"promises", "promises",
"each" "each"
], ],
"optionalDependencies": { "trulyOptionalDependencies": {
"bluebird": "^2.5.3" "bluebird": "^3.5.1"
}, },
"author": "AJ ONeal <coolaj86@gmail.com> (http://coolaj86.com/)", "author": "AJ ONeal <coolaj86@gmail.com> (https://coolaj86.com/)",
"license": "Apache2", "license": "(MIT OR Apache-2.0)",
"bugs": { "bugs": {
"url": "https://github.com/FuturesJS/forEachAsync/issues" "url": "https://git.coolaj86.com/coolaj86/foreachasync.js/issues"
}, },
"dependencies": { "dependencies": {}
}
} }

View File

@ -1,27 +1,32 @@
(function () { (function () {
"use strict"; "use strict";
var PromiseA = require('bluebird') var PromiseA = require('bluebird');
, forEachAsync = require('./forEachAsync').forEachAsync var forEachAsync = require('./forEachAsync').forEachAsync;
, 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) {
// test that array order is as expected console.log(i, '/', arr.length, 'began');
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
return PromiseA.resolve(); result = PromiseA.resolve();
} else { } else {
// test asynchronous callbacks // test asynchronous callbacks
return new Promise(function (resolve/*, reject*/) { result = new PromiseA(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,26 +1,30 @@
(function () { (function () {
"use strict"; "use strict";
var forEachAsync = require('./forEachAsync').forEachAsync.create(Promise) /* globals Promise */
, context = {} var forEachAsync = require('./forEachAsync').forEachAsync.create(Promise);
; var context = {};
forEachAsync([0, 500, 70, 200, 400, 100], function (element, i, arr) { forEachAsync([0, 500, 70, 200, 400, 100], function (element, i, arr) {
// test that array order is as expected var p;
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
return Promise.resolve(); p = Promise.resolve();
} else { } else {
// test asynchronous callbacks // test asynchronous callbacks
return new Promise(function (resolve/*, reject*/) { p = 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);