FAQ: Async Await - The await Operator

This community-built FAQ covers the “The await Operator” exercise from the lesson “Async Await”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Asynchronous JavaScript

FAQs on the exercise The await Operator

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

This might just be flying over my head, but I’m a bit confused with the output of this exercise.

From what I understand async… await lets us control the flow of our tasks by halting the execution of our async function until a given promise is resolved. Ok cool I can get that.

However, while working on the exercise and calling both the native promise (nativePromiseDinner()) and async (announceDinner()) functions together I see them execute identically.

There are 2 ways that this could make sense to me:

  1. After looking over the brainstormDinner Promise, each log is individually resolving the Promise, therefore completing the await, then returning to the next log.

  2. I don’t understand async… await and the exercise.

1 Like

Hi @sasquiche you are right that await lets us control the flow of our tasks by halting the execution of our async function until a given promise is resolved.

So if we declare that:

let resolvedValue = await brainstormDinner();

in the announceDinner function, we have to wait for the brainstormDinner promise to resolve.

Now, as you rightly mention, both the native promise (nativePromiseDinner()) and async (announceDinner()) functions execute identically. This is because the exercise is meant to show us that the async wait method is identical to using native promises.

Finally, looking over the brainstormDinner Promise, each log is is not resolving the Promise. Essentially, the promise first logs various messages, then resolves with the meal you decide to cook i.e

resolve('beans')

Hope that explanation helps.

1 Like

@textninja83556 This was the explanation I was looking for. Thank you very much.

2 Likes

function nativePromiseDinner() {
brainstormDinner().then((resolve) => {
console.log(I'm going to make ${resolve} for dinner.);
})
}
nativePromiseDinner() ;

// async/await version:
async function announceDinner() {
// Write your code below:
let meal = await brainstormDinner();
console.log(I'm going to make ${meal} for dinner.);
}

announceDinner()

I have called both, promises and async await at same tme and i got this as op why?faq

what i have understood is its because both of them are asynchronous function.so when compiler read nativePromiseDinner() , it do not stop and wait for it’s execution to be completed, rather it is running in background and compiler move to next function call which is announceDinner(), thus our both functions are running at same time in background with delay of 1sec(1000) that is why we get same statement 2 time, P.S sorry for my bad English :smiley:

In the previous lesson, they said:

async functions always return a promise.

So why do we need to use a promise anyway ?
Can’t we just do an async function and an await outside of it ?

This video on YouTube was linked earlier in the course. It’s about a half hour but really worth a watch to get exactly what is going on.