Skip to content

Instantly share code, notes, and snippets.

@briangonzalez
Created August 4, 2017 05:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briangonzalez/1a8d54e5081d861eaf5f104fce61e751 to your computer and use it in GitHub Desktop.
Save briangonzalez/1a8d54e5081d861eaf5f104fce61e751 to your computer and use it in GitHub Desktop.
Parellelizing multiple API requests.
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)
})()
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