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.
const brainstormDinner = require('./library.js')
// Native promise version:
function nativePromiseDinner() {
brainstormDinner().then((meal) => {
console.log(`I'm going to make ${meal} for dinner.`);
})
}
// async/await version:
async function announceDinner() {
// Write your code below:
let resValue = await brainstormDinner();
console.log(`I'm going to make ${resValue} for dinner.`);
}
announceDinner();
/*
this is the brainstormDinner function. It's a little silly. It returns a promise that uses a series of setTimeout() functions to simulate a time-consuming asynchronous action. It's a good example of "callback hell" or "the pyramid of doom," two ways people describe how confusing a bunch of nested callback functions can become.
*/
const brainstormDinner = () => {
return new Promise((resolve, reject) => {
console.log(`I have to decide what's for dinner...`)
setTimeout(() => {
console.log('Should I make salad...?')
setTimeout(() => {
console.log('Should I make ramen...?')
setTimeout(() => {
console.log('Should I make eggs...?')
setTimeout(() => {
console.log('Should I make chicken...?')
resolve('beans')
}, 1000)
}, 1000)
}, 1000)
}, 1000)
})
}
module.exports = brainstormDinner
const {shopForBeans, soakTheBeans, cookTheBeans} = require('./library.js');
// Write your code below:
async function makeBeans() {
let type = await shopForBeans();
let isSoft = await soakTheBeans(type);
let dinner = await cookTheBeans(isSoft);
console.log(dinner);
}
makeBeans();
/*
This is the shopForBeans function from the last exercise
*/
const shopForBeans = () => {
return new Promise((resolve, reject) => {
const beanTypes = ['kidney', 'fava', 'pinto', 'black', 'garbanzo'];
setTimeout(()=>{
let randomIndex = Math.floor(Math.random() * 5)
let beanType = beanTypes[randomIndex];
console.log(`I bought ${beanType} beans because they were on sale.`)
resolve(beanType);
}, 1000)
})
}
let soakTheBeans = (beanType) => {
return new Promise((resolve, reject) => {
console.log('Time to soak the beans.')
setTimeout(()=>{
console.log(`... The ${beanType} beans are softened.`)
resolve(true)
}, 1000)
})
}
let cookTheBeans = (isSoftened) => {
return new Promise((resolve, reject) => {
console.log('Time to cook the beans.')
setTimeout(()=>{
if (isSoftened) {
console.log('... The beans are cooked!')
resolve('\n\nDinner is served!')
}
}, 1000)
})
}
module.exports = {shopForBeans, soakTheBeans, cookTheBeans}