putil-waterfall
Advanced tools
Comparing version 1.0.8 to 1.1.0
@@ -17,4 +17,3 @@ /* putil-waterfall | ||
callback = callback || function() { | ||
}; | ||
callback = callback || function() {}; | ||
@@ -24,28 +23,28 @@ if (!funcs.length) | ||
let index = -1; | ||
var index = -1; | ||
const next = function(error) { | ||
if (error) | ||
return callback(error); | ||
function next(error, ...args) { | ||
if (error) { | ||
callback(error); | ||
return; | ||
} | ||
const args = Array.prototype.slice.call(arguments, 1); | ||
index++; | ||
const fn = funcs[index]; | ||
if (fn) { | ||
setImmediate(() => { | ||
setImmediate(function() { | ||
try { | ||
const o = fn(next, ...args); | ||
if (o && (o instanceof Promise || | ||
(typeof o.then === 'function' && | ||
typeof o.catch === 'function'))) { | ||
o.catch(err => { | ||
callback(err); | ||
const o = fn.apply(fn, [next].concat(args)); | ||
// If promise | ||
if (o && typeof o === 'object' && | ||
typeof o.then === 'function' && typeof o.catch === 'function') { | ||
o.catch(function(e) { | ||
callback.apply(callback, [e].concat(args)); | ||
}); | ||
} | ||
} catch (e) { | ||
callback(e); | ||
callback.apply(callback, [e].concat(args)); | ||
} | ||
}); | ||
} else callback(undefined, ...args); | ||
} | ||
} else | ||
callback.apply(callback, [undefined].concat(args)); | ||
}; | ||
@@ -55,2 +54,39 @@ next(null); | ||
waterfall.every = function(arr, fn, callback) { | ||
if (callback && typeof callback !== 'function') | ||
throw new Error('Invalid callback argument'); | ||
if (!Array.isArray(arr)) | ||
throw new Error('Invalid argument. Array value are required as first argument'); | ||
callback = callback || function() {}; | ||
var index = -1; | ||
const next = function(error) { | ||
if (error) | ||
return callback(error); | ||
if (!arr.length) | ||
return callback(); | ||
const v = arr.shift(); | ||
const args = Array.prototype.slice.call(arguments, 1); | ||
index++; | ||
setImmediate(function() { | ||
try { | ||
const o = fn.apply(fn, [next, v, index].concat(args)); | ||
// If promise | ||
if (o && typeof o === 'object' && | ||
typeof o.then === 'function' && typeof o.catch === 'function') { | ||
o.catch(function(e) { | ||
callback.apply(callback, [e, v, index].concat(args)); | ||
}); | ||
} | ||
} catch (e) { | ||
callback.apply(callback, [e, v, index].concat(args)); | ||
} | ||
}); | ||
}; | ||
next(null); | ||
}; | ||
module.exports = waterfall; |
{ | ||
"name": "putil-waterfall", | ||
"description": "Simple, fast async waterfall NodeJs module for ES6.", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"author": "Panates Ltd.", | ||
@@ -23,2 +23,3 @@ "contributors": [ | ||
"babel-eslint": "^7.2.3", | ||
"bluebird": "^3.5.0", | ||
"eslint": "^3.19.0", | ||
@@ -25,0 +26,0 @@ "eslint-config-google": "^0.8.0", |
@@ -61,5 +61,31 @@ # putil-waterfall | ||
`waterfall.every(array, fn, callback)` | ||
**array:** Any array | ||
**fn:** Function to be called for every value. `fn(next, value, index) | ||
**callback:** An optional callback to run once iteration completed. | ||
```javascript | ||
const waterfall = require('putil-waterfall'); | ||
var total = 0; | ||
waterfall.every([1, 2, 3, 4], | ||
function(next, val) { | ||
total += val; | ||
next(null); | ||
}, | ||
function(err) { | ||
console.log('Total: ' + total); | ||
}); | ||
``` | ||
Result output | ||
``` | ||
Total: 10 | ||
``` | ||
## Node Compatibility | ||
- node `>= 6.x`; | ||
- node `>= 4.0`; | ||
@@ -66,0 +92,0 @@ ### License |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8207
75
110
6
1