Skip to content

Instantly share code, notes, and snippets.

@umkasanki
Last active May 1, 2020 10:17
Show Gist options
  • Save umkasanki/b8aed85e6fb1e3f732c0b35977ef3956 to your computer and use it in GitHub Desktop.
Save umkasanki/b8aed85e6fb1e3f732c0b35977ef3956 to your computer and use it in GitHub Desktop.
fetch data from one or multiple urls
// store urls to fetch in an array
const urls = [
'/ajax/getLanguagesRate?sourceLang=arabic&targetLang=albanian',
'/ajax/getLanguagesRate?sourceLang=arabic&targetLang=afrikaans',
'/ajax/getSubjectRate?subject=legal',
'/ajax/getDeliveryRate?delivery=standart',
];
// from multiple urls
Promise.all(urls.map((url) => fetch(url)))
.then((resp) => Promise.all(resp.map((r) => r.json())))
.then((result) => {
// ...
console.log('multiple rates data', result);
}).catch((error) => {
// if there's an error, log it
console.log(error);
});
// from single url
fetch(urls[0]).then(
(response) => response.json(),
).then((jsonData) => { // handle json data processing here
console.log('rate jsonData', jsonData);
}).catch((error) => {
// if there's an error, log it
console.log(error);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment