Create a gist now

Instantly share code, notes, and snippets.

@caike /run.js
Last active Aug 29, 2015

async calls
fetchProfile(1, function(){
console.log("Callback 1");
});
fetchProfile(2, function(){
console.log("Callback 2");
});
fetchProfile(3, function(){
console.log("Callback 3");
});
fetchProfile(4, function(){
console.log("Callback 4");
});
fetchProfile(5, function(){
console.log("Callback 5");
});
fetchProfile(6, function(){
console.log("Callback 6");
});
fetchProfile(7, function(){
console.log("Callback 7");
});
fetchProfile(8, function(){
console.log("Callback 8");
});
function fetchProfile(n, callback){
console.log("Running ", n);
if(n === 2){
setTimeout(function(){
callback();
}, 800);
}else{
setTimeout(function(){
callback();
}, 200);
}
}
/*
Output:
Running 1
Running 2
Running 3
Running 4
Running 5
Running 6
Running 7
Running 8
Callback 1
Callback 3
Callback 4
Callback 5
Callback 6
Callback 7
Callback 8
Callback 2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment