Skip to content

Instantly share code, notes, and snippets.

@eslam-mahmoud
Last active June 26, 2019 15:25
Show Gist options
  • Save eslam-mahmoud/fbb84cf745d9d753ec923a163855ccfd to your computer and use it in GitHub Desktop.
Save eslam-mahmoud/fbb84cf745d9d753ec923a163855ccfd to your computer and use it in GitHub Desktop.
Multiple Simultaneous Ajax Requests (concurrent sequential requests)
var ids = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
var stop = false;
function go(index, step) {
if (stop) {
console.log('Stoped', index, step);
return;
}
if (index > ids.length) {
console.log('Done');
return;
}
id = ids[index];
fetch('https://api.example.com/product/='+id, {
method: 'GET',
credentials: 'include'
})
.then((response) => response.json())
.then((json) => {
console.log(index, id, json);
if (!json.error) {
go(index+step, step);
}
}).catch((err) => {
console.log(index, id, err);
});
}
go(0, 3);
go(1, 3);
go(2, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment