fetch() Promise.all()
by Gustavo
JavaScript
async function getData() {
try {
const url1 = 'https://jsonplaceholder.typicode.com/todos/1';
const url2 = 'https://jsonplaceholder.typicode.com/todos/2';
const url3 = 'https://jsonplaceholder.typicode.com/todos/3';
// Results will be an array of promises
const results = await Promise.all([
fetch(url1),
fetch(url2),
fetch(url3)
]);
// Otro array de promises
const dataPromises = results.map(result => result.json());
const data = await Promise.all(dataPromises);
return data; // it will return a promise because it's an async function
} catch (err) {
throw new Error(err);
}
}
const data = getData().then((data) => { data.map(todo => console.log(todo.title)) });