Skip to content

Instantly share code, notes, and snippets.

@Alexzanderk
Last active July 2, 2019 21:00
Show Gist options
  • Save Alexzanderk/5a74d3584842c12510cc3715d807545d to your computer and use it in GitHub Desktop.
Save Alexzanderk/5a74d3584842c12510cc3715d807545d to your computer and use it in GitHub Desktop.
delay promises
function requestAllWithDelay (urls, delay) {
return urls.reduce((promise, url) => {
return promise
.then((responses) => {
return fetch(url) // Or whatever request library you're using. If it doesn't support promises, you can wrap it in `new Promise((resolve, reject) => someLib(url, { onSuccess: resolve, onError: reject }));` or something similar.
.then(response => {
return new Promise(resolve => {
setTimeout(resolve, delay, responses.concat(response)); // replies.concat might not work, depending on how you want to accumulate all the data. Maybe you don't even care about the responses?
})
})
})
}, Promise.resolve([]))
}
requestAllWithDelay(['http://...', ...], 1000)
.then(allResponsesInAnOrderedArray => { ... })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment