Created
December 24, 2010 02:54
-
-
Save chrisdickinson/753843 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
// call this when we're done. | |
var done = function(err, result) { | |
console.log(result); | |
}; | |
var EXPENSIVE_OPERATION_ON = function(data, callback) { | |
callback(/*error, data, etc*/); | |
}; | |
var arr = [1,2,3,4], | |
i = -1, | |
len = arr.length, | |
results = [], | |
eterator = function(err, result) { | |
// store this reference for later. | |
var callee = arguments.callee; | |
if(err) { | |
done(err); | |
} else { | |
result && results.push(result); | |
++i; | |
if(i < len) { | |
// release control to the event loop using setTimeout | |
// this also resets the stack. | |
setTimeout(function() { | |
EXPENSIVE_OPERATION_ON(arr[i], callee); | |
}); | |
} else { | |
done(results); | |
} | |
} | |
}; | |
eterator(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment