Mutiple asynchronous request
Using promise.all and async await
by vaheed akhtar
CSS
body{
background: white;
}
JavaScript
// function scaryClown() {
// let data =
// fetch('https://jsonplaceholder.typicode.com/users')
// .then(res=>res.json())
// .catch(err=>console.log(err));
// return data;
// }
// async function msg() {
// const msg = await scaryClown();
// console.log('Message:', msg);
// }
// msg();
function who() {
return fetch('https://jsonplaceholder.typicode.com/users/1')
.then(res=>res.json())
.catch(err=>console.log(err));
}
function what() {
return fetch('https://jsonplaceholder.typicode.com/albums/1')
.then(res=>res.json())
.catch(err=>console.log(err));
}
function where() {
let data =
fetch('https://jsonplaceholder.typicode.com/posts/1')
.then(res=>res.json())
.catch(err=>console.log(err));
return data;
}
async function msg() {
// const a = await who();
// const b = await what();
// const c = await where();
//or
const [a, b, c] = await Promise.all([who(), what(), where()]);
document.write(`${ a.name }--- ${ b.title }-- ${ c.title }`).sty;
}
msg();