Last active
August 29, 2015 14:05
-
-
Save rezen/ea4aa89b0e98e936442a to your computer and use it in GitHub Desktop.
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
var series = [ | |
function( callback ){ | |
callback( 'bob', 12); | |
return 1; | |
}, | |
function ( callback, person, age ) { | |
console.log( person,age); | |
callback( 'moew' ); | |
return 12; | |
}, | |
function( callback, eh ){ | |
console.log(eh); | |
callback( 'moew2' ); | |
return 10; | |
}, | |
function( message ){ | |
console.log( message ); | |
return 5; | |
} | |
]; | |
function callbackSeries( callbacks, complete ){ | |
var max = callbacks.length; | |
var callbackList = []; | |
var results = []; | |
for (var i= 0, len = max; i < len; ++i) { | |
callbackList[i] = makeCallback( i ); | |
}; | |
function makeCallback( index ){ | |
index = parseInt( index ); | |
var next = 1 + index; | |
var args = []; | |
return function(){ | |
args = Array.prototype.slice.call( arguments, 0); | |
if( callbackList[ next ] ){ | |
args.unshift( callbackList[ next ] ); | |
} else { | |
isLast = true; | |
} | |
results.push( callbacks[index].apply( null, args ) ); | |
if( isLast ){ | |
if( typeof complete === 'function') complete( results ); | |
} | |
} | |
} | |
return callbackList[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment