Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function asyncQueue(/* function, function, ... */) {
- var slice = Array.prototype.slice,
- funcs = slice.call(arguments, 0),
- baseArgs = [function() {
- next(arguments);
- }],
- next = function(args) {
- var nextFunc = funcs.shift();
- nextFunc && nextFunc.apply(
- this, baseArgs.concat( slice.call(args, 0) )
- );
- };
- next();
- };
- // eg...
- asyncQueue(function(next) {
- console.log('1');
- setTimeout(next, 1000);
- }, function(next) {
- console.log('2');
- setTimeout(next, 1000);
- }, function(next) {
- console.log('3');
- setTimeout(next, 1000);
- }, function(next) {
- console.log('4');
- setTimeout(next, 1000);
- });
Add Comment
Please, Sign In to add comment