Skip to content

Instantly share code, notes, and snippets.

@adarsh-chakraborty
Created December 9, 2021 09:54
Show Gist options
  • Select an option

  • Save adarsh-chakraborty/7b19af6b24a97ed323344a3efcd2b4e1 to your computer and use it in GitHub Desktop.

Select an option

Save adarsh-chakraborty/7b19af6b24a97ed323344a3efcd2b4e1 to your computer and use it in GitHub Desktop.
Fetch data with Promise concurrently
let fetchData = async () => {
const res = await fetch('https://bookshelf.gq/api/books');
const data = await res.json();
console.log('data is ready', data);
const res2 = await fetch('https://dikz.herokuapp.com/api/latest');
const data2 = await res2.json();
console.log('data2 is ready!', data2);
};
let fetchDataConcurrent = async () => {
const res = fetch('https://bookshelf.gq/api/books').then((res) => res.json());
const res2 = fetch('https://dikz.herokuapp.com/api/latest').then((res) =>
res.json()
);
const data = await Promise.all([res, res2]);
console.log('data is ready', data[0]);
console.log('data2 is ready!', data[1]);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment