Code sample

Quickly mapping an array of URLs to responses with JavaScript async/await and Promise.all

While perhaps not the most readable, a compact version (using window.fetch) can look like this:

const urls = [
"https://jsonplaceholder.typicode.com/comments/1",
"https://jsonplaceholder.typicode.com/comments/2",
"https://jsonplaceholder.typicode.com/comments/3"
];
async function fetchAll() {
const results = await Promise.all(urls.map((url) => fetch(url).then((r) => r.json())));
console.log(JSON.stringify(results, null, 2));
}
fetchAll();
Joel Abrahamsson

Joel Abrahamsson

I'm a passionate web developer and systems architect living in Stockholm, Sweden. I work as CTO for a large media site and enjoy developing with all technologies, especially .NET, Node.js, and ElasticSearch. Read more

More about Programming