This commit is contained in:
AJ ONeal 2018-03-27 23:41:41 -06:00
parent ba81917d4f
commit 812950328d
4 changed files with 32 additions and 37 deletions

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 () {
@ -61,13 +59,14 @@
exports.create = forEachAsync.create = function () {}; exports.create = forEachAsync.create = function () {};
*/ */
/* globals Promise */
if ('undefined' !== typeof Promise) {
exp.forEachAsync = create(Promise);
}
else {
try { try {
exp.forEachAsync = create(require('bluebird')); exp.forEachAsync = create(require('bluebird'));
} catch(e) { } catch(e) {
if ('undefined' !== typeof Promise) {
exp.forEachAsync = create(Promise);
} else {
try { try {
exp.forEachAsync = create(require('es6-promise')); exp.forEachAsync = create(require('es6-promise'));
} catch(e) { } catch(e) {
@ -87,4 +86,4 @@
}; };
exports.forEachAsync.create = create; exports.forEachAsync.create = create;
}('undefined' !== typeof exports && exports || new Function('return this')())); }('undefined' !== typeof exports && exports || window));

View File

@ -1,9 +1,9 @@
{ {
"name": "foreachasync", "name": "foreachasync",
"version": "5.0.5", "version": "5.1.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://github.com/FuturesJS/forEachAsync", "homepage": "https://git.coolaj86.com/coolaj86/foreachasync.js",
"main": "forEachAsync.js", "main": "foreachasync.js",
"directories": { "directories": {
"test": "test" "test": "test"
}, },
@ -12,7 +12,7 @@
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/FuturesJS/forEachAsync.git" "url": "git://git.coolaj86.com/coolaj86/foreachasync.js.git"
}, },
"keywords": [ "keywords": [
"futuresjs", "futuresjs",
@ -26,13 +26,12 @@
"each" "each"
], ],
"optionalDependencies": { "optionalDependencies": {
"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,16 +1,14 @@
(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) {
console.log(i, '/', arr.length, 'began'); console.log(i, '/', arr.length, 'began');
var result var result;
;
// test that thisness is applied // test that thisness is applied
this[element] = i; this[element] = i;
@ -20,7 +18,7 @@
result = PromiseA.resolve(); result = PromiseA.resolve();
} else { } else {
// test asynchronous callbacks // test asynchronous callbacks
result = new Promise(function (resolve/*, reject*/) { result = new PromiseA(function (resolve/*, reject*/) {
setTimeout(resolve, element); setTimeout(resolve, element);
}); });
} }

View File

@ -1,13 +1,12 @@
(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) {
var p var p;
;
// test that thisness is applied // test that thisness is applied
this[element] = i; this[element] = i;