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 }