Last active
May 1, 2020 10:17
-
-
Save umkasanki/b8aed85e6fb1e3f732c0b35977ef3956 to your computer and use it in GitHub Desktop.
fetch data from one or multiple urls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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