Last active
June 26, 2019 15:25
-
-
Save eslam-mahmoud/fbb84cf745d9d753ec923a163855ccfd to your computer and use it in GitHub Desktop.
Multiple Simultaneous Ajax Requests (concurrent sequential requests)
This file contains 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
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