Skip to content

Instantly share code, notes, and snippets.

@baamenabar
Created March 17, 2016 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baamenabar/c1f019f7f057307e9ed7 to your computer and use it in GitHub Desktop.
Save baamenabar/c1f019f7f057307e9ed7 to your computer and use it in GitHub Desktop.
async each for an array
/**
* for each item in the array, call a function, but only after the previous has called the callback.
**/
function consecutive (list, task) {
var count = 0;
function step() {
var item = list[count];
//console.log(count++);
if(typeof item === 'undefined') {
// console.log('this istem is undefined');
return;
}
response = task(item, step);
}
step();
}
/**
* usage
**/
consecutive([5,8,4,6,5,7], function(item, cb){
$.get( "ajax/test.php?amount="+item, function( data ) {
console.log( "Load was performed.", data );
cb();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment