Skip to content

Instantly share code, notes, and snippets.

@mingzhi22
Last active April 14, 2016 04:21
Show Gist options
  • Save mingzhi22/ad4b29935e872221e684 to your computer and use it in GitHub Desktop.
Save mingzhi22/ad4b29935e872221e684 to your computer and use it in GitHub Desktop.
var arr = [1, 2, 3, 4, 5, 6],
len = arr.length;
var stack = function() {
console.log('all done');
};
while(len--) {
var elem = arr[len];
stack = (function(elem, callback) {
return function() {
console.debug(elem);
setTimeout(callback, 2000);
};
}(elem, stack));
}
stack();
var arr = [1, 2, 3, 4, 5, 6],
i = 0,
len = arr.length;
var step = function(elem, callback) {
console.log(elem);
setTimeout(function() {
callback();
}, 2000);
};
if(i < len) {
step(arr[i], function() {
i++;
if(i < len) {
step(arr[i], arguments.callee);
} else {
console.log('all done');
}
});
} else {
console.log('all done');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment