fetch api fetch from multiple url code example
Example 1: fetch multiple urls javascript
async function getAllUrls(urls) {
try {
var data = await Promise.all(
urls.map(
url =>
fetch(url).then(
(response) => response.json()
)));
return (data)
} catch (error) {
console.log(error)
throw (error)
}
}
Example 2: how to make multiple fetch requests
Promise.all([
fetch('https://jsonplaceholder.typicode.com/posts'),
fetch('https://jsonplaceholder.typicode.com/users')
]).then(function (responses) {
return Promise.all(responses.map(function (response) {
return response.json();
}));
}).then(function (data) {
console.log(data);
}).catch(function (error) {
console.log(error);
});