Last active
July 2, 2019 21:00
-
-
Save Alexzanderk/5a74d3584842c12510cc3715d807545d to your computer and use it in GitHub Desktop.
delay promises
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
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