Created
August 4, 2017 05:22
-
-
Save briangonzalez/1a8d54e5081d861eaf5f104fce61e751 to your computer and use it in GitHub Desktop.
Parellelizing multiple API 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
const Promise = require('bluebird'); | |
const getResponses = async (urls) => { | |
const requests = urls.map(u => fetch /* fetch is a newish builtin API */) | |
const responses = await Promise.all(requests) | |
return responses | |
} | |
const urls = [ | |
'https://foo.com/bar.json', | |
'https://foo.com/baz.json', | |
'https://foo.com/qux.json' | |
] | |
(async () => { | |
const responses = getResponses(urls) | |
})() |
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
const getResponses = async (urls) => { | |
const requests = urls.map(u => fetch /* fetch is a newish builtin API */) | |
const responses = [] | |
for (let url of urls) { | |
const response = await fetch(url) | |
responses.push(response) | |
} | |
return responses | |
} | |
const urls = [ | |
'https://foo.com/bar.json', | |
'https://foo.com/baz.json', | |
'https://foo.com/qux.json' | |
] | |
(async () => { | |
const responses = getResponses(urls) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment