function getBook(bookId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
let book = {
title: ‘JavaScript’,
authorId: ‘123’
}
resolve(book);
}, 500);
});
}
function getAuthor(authorId) {
return new Promise((resolve, reject) => {
setTimeout(() => {
let author = {
name: ‘Amy’,
age: 28
}
resolve(author);
}, 500);
});
}
async function fetchData() {
let book = await getBook(2020);
let author = await getAuthor(book.authorId);
console.log(author)
}
fetchData(); // { name: ‘Amy’, age: 28 }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter