Comparing version 1.4.8 to 1.5.0
function easyFor(obj, func, concurrency, last){ | ||
var max = 0; | ||
var argsLen = arguments.length; | ||
var isArr = obj instanceof Array; | ||
var isNum = false; | ||
var keys = null; | ||
var isEnd = false; | ||
var isWaterfall = (func === undefined || typeof func === 'function') && isWaterfallCall(obj); | ||
if(obj instanceof Array) max = obj.length; | ||
@@ -20,3 +23,3 @@ else if(obj instanceof Object) | ||
if((arguments.length === 3 || arguments.length === 4) && typeof func === 'number' && typeof concurrency === 'function') | ||
if((argsLen === 3 || argsLen === 4) && typeof func === 'number' && typeof concurrency === 'function') | ||
{ | ||
@@ -36,3 +39,8 @@ var temp = func; | ||
else concurrency = parseInt(concurrency); | ||
} | ||
} | ||
if(isWaterfall && typeof func === 'function') | ||
{ | ||
last = func; | ||
func = null; | ||
} | ||
var count = {start:0, end:0, processing:0}; | ||
@@ -43,10 +51,19 @@ var next = function(err){ | ||
if(count.end >= max || err) | ||
{ | ||
if(last) | ||
{ | ||
{ | ||
if(last && !isEnd) | ||
{ | ||
if(err) last(err); | ||
else last(); | ||
else | ||
{ | ||
if(isWaterfall) last.apply(this, Array.from(arguments)); | ||
else last(); | ||
} | ||
} | ||
isEnd = true; | ||
} | ||
else run(); | ||
else | ||
{ | ||
if(isWaterfall) run.apply(this, Array.prototype.slice.call(arguments, 1)); | ||
else run(); | ||
} | ||
}; | ||
@@ -57,3 +74,7 @@ var run = function(){ | ||
count.processing++; | ||
if(isArr) func(count.start, obj[count.start++], next); | ||
if(isArr) | ||
{ | ||
if(isWaterfall) obj[count.start++].apply(this, Array.from(arguments).concat(next)); | ||
else func(count.start, obj[count.start++], next); | ||
} | ||
else if(isNum) func(count.start++, next); | ||
@@ -66,2 +87,17 @@ else func(keys[count.start], obj[keys[count.start++]], next); | ||
} | ||
function isWaterfallCall(arr){ | ||
var result = arr instanceof Array; | ||
if(result) | ||
{ | ||
for(var a of arr) | ||
{ | ||
if(typeof a !== 'function') | ||
{ | ||
result = false; | ||
break; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
module.exports = easyFor; |
@@ -20,7 +20,7 @@ /** | ||
} | ||
loop["for"] = loop["while"] = loop["loop"] = loop["forEach"] = loop; | ||
loop["waterfall"] = loop["for"] = loop["while"] = loop["loop"] = loop["forEach"] = loop; | ||
loop["create"] = easyManual; | ||
loop["series"] = easyAsync.series; | ||
loop["parallel"] = easyAsync.parallel; | ||
loop["parallel"] = easyAsync.parallel; | ||
module.exports = loop; |
{ | ||
"name": "easy-loop", | ||
"version": "1.4.8", | ||
"version": "1.5.0", | ||
"description": "Easy sync loop processing for Node.js", | ||
@@ -10,3 +10,3 @@ "main": "lib/easy-loop", | ||
"keywords": [ | ||
"serial","loop","forEach","for","while","sync","easy loop","easy for","easy while","sync loop","iteration","step by step","concurrency","async","series","parallel" | ||
"serial","loop","forEach","for","while","sync","easy loop","easy for","easy while","sync loop","iteration","step by step","concurrency","async","series","parallel","waterfall" | ||
], | ||
@@ -13,0 +13,0 @@ "repository": { |
@@ -23,3 +23,3 @@ | ||
## series or parallel function | ||
## series or parallel or waterfall function | ||
1) Method | ||
@@ -48,5 +48,23 @@ var loop = require('easy-loop'); | ||
loop.waterfall([ | ||
function(callback){ | ||
var arg1 = 1; | ||
callback(null, arg1); | ||
}, | ||
function(arg, callback){ | ||
//console.log(arg) => 1 | ||
var arg1 = 2; | ||
callback(null, arg, arg1); | ||
}, | ||
function(arg1, arg2, callback){ | ||
//console.log(arg1, arg2) => 1, 2 | ||
var arg1 = 3; | ||
callback(null, arg1); | ||
}], function(err, results){ | ||
//err : undefined | ||
//console.log(results) => 3 | ||
}); | ||
2) Arguments | ||
(1) Array or Object - require | ||
(1) Array or Object (waterfall only Array) - require | ||
(2) callback function - option | ||
@@ -53,0 +71,0 @@ |
@@ -45,5 +45,5 @@ //var fs = require('fs'); | ||
console.log("Case3 End"); | ||
*/ | ||
/* | ||
var obj = {a:1,b:2,c:3,d:4,e:5}; | ||
@@ -61,8 +61,8 @@ console.log("Case4 Start => Object and 3 arguments"); | ||
console.log("Case4 End"); | ||
*/ | ||
/* | ||
var arr = []; | ||
@@ -303,3 +303,3 @@ console.log("Case5 Start => Empty Array and 3 arguments"); | ||
/* | ||
console.log("Case15 Start => Array(or Object). Like as 'async.series'. But sub functions run parallel."); | ||
@@ -337,2 +337,3 @@ console.log("Start : Now Second : ", new Date().getSeconds()); | ||
console.log("Case15 End"); | ||
*/ | ||
@@ -351,2 +352,41 @@ /* | ||
Result : Now Second : 2 | ||
*/ | ||
/* | ||
console.log("Case16 Start => Array. Like as 'async.waterfall'. If the error is when to stop."); | ||
console.log("Start : Now Second : ", new Date().getSeconds()); | ||
var arr = [ | ||
function(callback){ | ||
console.log("start waterfall", arguments); | ||
callback(null, 1); | ||
}, | ||
function(num1, callback){ | ||
setTimeout(function(){ | ||
console.log(100); | ||
callback(null, num1, 100); | ||
}, 500); | ||
}, function(num1, num2, callback){ | ||
setTimeout(function(){ | ||
console.log(200); | ||
callback(null, num1, num2, 200); | ||
}, 500); | ||
} | ||
]; | ||
loop.waterfall(arr, function(err, num1, num2, num3){ | ||
console.log("result : ", JSON.stringify(arguments)); | ||
console.log("Result : Now Second : ", new Date().getSeconds()); | ||
}); | ||
console.log("Case16 End"); | ||
*/ | ||
/* | ||
Case16 Start => Array. Like as 'async.waterfall'. If the error is when to stop. | ||
Start : Now Second : 14 | ||
start waterfall { '0': [Function] } | ||
Case16 End | ||
100 | ||
200 | ||
result : {"0":null,"1":1,"2":100,"3":200} | ||
Result : Now Second : 15 | ||
*/ |
31308
600
594