Skip to content

Instantly share code, notes, and snippets.

@srdjanRakic
Created August 13, 2018 18:46
Show Gist options
  • Save srdjanRakic/e615d6ed032c3c7c551feff6f46f7b1c to your computer and use it in GitHub Desktop.
Save srdjanRakic/e615d6ed032c3c7c551feff6f46f7b1c to your computer and use it in GitHub Desktop.
Sync up multiple Promises to a single result set
fetchData = () => {
const urls = [
"https://jsonplaceholder.typicode.com/posts/1",
"https://jsonplaceholder.typicode.com/posts/2",
"https://jsonplaceholder.typicode.com/posts/3",
"https://jsonplaceholder.typicode.com/posts/4",
"https://jsonplaceholder.typicode.com/posts/5",
"https://jsonplaceholder.typicode.com/posts/6",
"https://jsonplaceholder.typicode.com/posts/7",
"https://jsonplaceholder.typicode.com/posts/8"
];
const allRequests = urls.map(url =>
fetch(url).then(response => response.json())
);
return Promise.all(allRequests);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment