Created
March 17, 2016 13:43
-
-
Save baamenabar/c1f019f7f057307e9ed7 to your computer and use it in GitHub Desktop.
async each for an array
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
/** | |
* 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