Last active
August 29, 2015 14:02
-
-
Save catdad/fc77d3e02e1f3687ed50 to your computer and use it in GitHub Desktop.
Based on the original sync gist -- https://gist.github.com/catdad/4598289 -- this code will execute asynchronous functions synchronously, passing the result of one function as the seed data of the next.
This file contains hidden or 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
//simple sync function | |
function sync(/* funcsArray, done, seedDataArgs */) { | |
var args = Array.prototype.slice.call(arguments, 0); | |
//functions array to sync is first | |
var funcs = args.shift(); | |
//done callback is second | |
var done = args.shift(); | |
//all remaining arguments are the seed data | |
var counter = funcs.length; | |
var idx = 0; | |
var next = function () { | |
//console.log("inside next"); | |
var privateArgs = Array.prototype.slice.call(arguments, 0); | |
privateArgs.push(next); | |
if (--counter === 0) done && done.apply(null, privateArgs); | |
else funcs[idx++].apply(null, privateArgs); | |
}; | |
//add the next callback as the last argument | |
args.push(next) | |
funcs[idx++].apply(null, args); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment