ASYNC AWAIT
Review
Awesome work getting the hang of the async...await syntax! Let’s review what you’ve learned:
async...await is syntactic sugar built on native JavaScript promises and generators.
We declare an async function with the keyword async.
Inside an async function we use the await operator to pause execution of our function until an asynchronous action completes and the awaited promise is no longer pending .
await returns the resolved value of the awaited promise.
We can write multiple await statements to produce code that reads like synchronous code.
We use try...catch statements within our async functions for error handling.
We should still take advantage of concurrency by writing async functions that allow asynchronous actions to happen in concurrently whenever possible.
let {cookBeans, steamBroccoli, cookRice, bakeChicken} = require('./library.js')
// Write your code below:
async function serveDinnerAgain() {
let foodArray = await Promise.all([steamBroccoli(), cookRice(), bakeChicken(), cookBeans()]);
console.log(`Dinner is served. We're having ${foodArray[0]}, ${foodArray[1]}, ${foodArray[2]}, and ${foodArray[3]}.`)
}
serveDinnerAgain()
let cookBeans = () => {
return new Promise ((resolve, reject) => {
setTimeout(()=>{
resolve('beans')
}, 1000)
})
}
let steamBroccoli = () => {
return new Promise ((resolve, reject) => {
setTimeout(()=>{
resolve('broccoli')
}, 1000)
})
}
let cookRice = () => {
return new Promise ((resolve, reject) => {
setTimeout(()=>{
resolve('rice')
}, 1000)
})
}
let bakeChicken = () => {
return new Promise ((resolve, reject) => {
setTimeout(()=>{
resolve('chicken')
}, 1000)
})
}
module.exports = {cookBeans, steamBroccoli, cookRice, bakeChicken}