Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Created December 24, 2010 02:54
Show Gist options
  • Save chrisdickinson/753843 to your computer and use it in GitHub Desktop.
Save chrisdickinson/753843 to your computer and use it in GitHub Desktop.
// 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