Created
December 14, 2015 03:08
-
-
Save pegasuskim/9b563736b75872e4d410 to your computer and use it in GitHub Desktop.
async customizing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# CustomAsync | |
CustomAsync | |
## Examples | |
```js | |
var CAsync = require('custom-async'); | |
var async = new CAsync(); | |
async.sequence([ | |
function(callback) { | |
console.log('first job'); | |
callback(null, 'second'); | |
}, | |
function(arg1, callback) { | |
console.log(arg1 + ' job'); | |
setTimeout(function() { | |
callback(null, 'third'); | |
}, 1000); | |
}, | |
function(arg1, callback) { | |
console.log(arg2 + ' job'); | |
callback(null, 'done'); | |
} | |
], function(err, result) { | |
console.log("result: " + result); | |
}); | |
*/ | |
function CustomAsync() { | |
this.nextTick = process.nextTick; | |
var _forEach = function(arr, iterator) { | |
if (arr.forEach) { | |
return arr.forEach(iterator); | |
} | |
for (var i = 0; i < arr.length; i++) { | |
iterator(arr[i], i, arr); | |
} | |
}; | |
this.forEach = function(arr, iterator, callback) { | |
callback = callback || function() {}; | |
if (!arr.length) { | |
return callback(); | |
} | |
var completed = 0; | |
_forEach(arr, function(x) { | |
iterator(x, function(err) { | |
if (err) { | |
callback(err); | |
callback = function() {}; | |
} else { | |
completed++; | |
if (completed == arr.length) { | |
callback(null); | |
} | |
} | |
}); | |
}); | |
}; | |
this.sequence = function(jobs, callback) { | |
callback = callback || function() {}; | |
if (!jobs.length) { | |
return callback(); | |
} | |
var wrapper = function(iter) { | |
return function(err) { | |
if (err) { | |
callback(err); | |
callback = function() {}; | |
} else { | |
var args = Array.prototype.slice.call(arguments, 1); | |
var next = iter.next(); | |
if (next) { | |
args.push(wrapper(next)); | |
} else { | |
args.push(callback); | |
} | |
process.nextTick(function() { | |
iter.apply(null, args); | |
}); | |
} | |
}; | |
}; | |
wrapper(this.iterator(jobs))(); | |
} | |
this.iterator = function(jobs) { | |
var makeCallback = function(index) { | |
var fn = function() { | |
if (jobs.length) { | |
jobs[index].apply(null, arguments); | |
} | |
return fn.next(); | |
}; | |
fn.next = function() { | |
return (index < jobs.length - 1) ? makeCallback(index + 1) : null; | |
}; | |
return fn; | |
}; | |
return makeCallback(0); | |
}; | |
this.up = function(start, end, step, work, callback) { | |
if (start > end) { | |
console.log('Invalid parameters'); | |
return; | |
} | |
step = Math.abs(step); | |
var loop = function(i) { | |
if (i >= end) { | |
if (typeof callback == 'function') { | |
process.nextTick(function() { callback(); }); | |
} | |
return; | |
} | |
work(i, function next() { | |
process.nextTick(function() { loop(i + step); }); | |
}, function done() { | |
process.nextTick(function() { loop(end); }); | |
}); | |
// process.nextTick(function() { loop(i + step); }); | |
}; | |
loop(start); | |
}; | |
} | |
module.exports = CustomAsync; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment