FAQ: Async Await - Handling Independent Promises

This community-built FAQ covers the “Handling Independent Promises” 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 Handling Independent Promises

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!

There’s no examples for this
“Note: if we have multiple truly independent promises that we would like to execute fully in parallel, we must use individual .then() functions and avoid halting our execution with await .”

Please help

15 Likes

This is very interesting.

Since await is just syntactic sugar for .then… why can’t we do individual async-await functions, just as we can do individual .then functions?

Well, it seems that we can:

I’m not showing the library code here, but I’ve only changed the setTimeouts - to complete in the reverse order that they are executed, as follows:

let {cookBeans, steamBroccoli, cookRice, bakeChicken} = require('./library.js')

// Write your code below:

async function cookStuff() {
 const cookFirst = await cookBeans();
 const cookSecond = await cookRice();
 console.log(`cooked ${cookFirst} (5 secs) and ${cookSecond} (1 sec)`);
}

async function bakeStuff() {
 const bakeFirst = await bakeChicken();
 console.log(`baked ${bakeFirst} (4 secs)`);
}

async function steamStuff() {
 const steamFirst = await steamBroccoli();
 console.log(`steamed ${steamFirst} (1 sec)`);
}


//run the async-await functions
cookStuff();
bakeStuff();
steamStuff();

Result
44

Summary
I think I am demonstrating that putting your awaiting promises in separately-called async functions, means that you can still run those functions in parallel; independently & non-concurrently.

And therefore that the quoted statement in the lesson text is not true?

2 Likes

This seems false: “Note: if we have multiple truly independent promises that we would like to execute fully in parallel, we must use individual .then() functions and avoid halting our execution with await .”

Why? Because if you have a .then() followed by another .then(), the second then will only execute once the first one has resolved. So the second .then() is dependent on the first then; it’s not appropriate for independent promises.

4 Likes

This line also confused me and brought me to this topic. I was expecting the line to suggest using Promise.all() instead at the very least.

5 Likes

Me, too. I think using then() to execute multiple independent promises at same time is confuse, since what we learned is then() is for chaining promises which depend on one another. I cannot imagine how you achieve the quote by then(). Anyone and have ideas?

1 Like

I have a question about page 7/9: Handling Independent Promises, specifically part 2.

Declare an async function, serveDinner() . Create four variables:

  • vegetablePromise which should be assigned the return value of steamBroccoli()
  • starchPromise which should be assigned the return value of cookRice()
  • proteinPromise which should be assigned the return value of bakeChicken()
  • and sidePromise which should be assigned the return value of cookBeans()

These variables should be assigned the promise objects themselves not their resolved values.

What does the last line imply? When invoked, don’t these functions automatically return resolved promise values? How would I return just “the promise objects themselves and not their resolved values”?? Thank you.

Here is the steamBroccoli function, for example:

let steamBroccoli = () => {
 return new Promise ((resolve, reject) => {
   setTimeout(()=>{
     resolve('broccoli')
   }, 1000)
 })
}
2 Likes

Isn’t it because .then () only chains executions, it doesn’t actually halt the execution of further code until the promise has resolved (which is what async…await does)?

So .then() will start execution of A and then execution of B which then run in parallel? I’m new to this as well!

2 Likes

I have written the solution according to what I have understood, someone please correct me if i’am wrong
async function serveDinner(){

let vegetablePromise = steamBroccoli();

let starchPromise = cookRice();

let proteinPromise = bakeChicken();

let sidePromise = cookBeans();

console.log(Dinner is served. We're having);

vegetablePromise.then(resovledValue=>{

console.log(resovledValue);})

starchPromise.then(resovledValue=>{

console.log(resovledValue);})

proteinPromise.then(resovledValue=>{

console.log(resovledValue);})

sidePromise.then(resovledValue=>{

console.log(resovledValue);})

}

serveDinner();

This is so sad, no one bothers to answer the questions above ;-;

4 Likes

it happened again, I have correct code, running smoothly and to the level’s expectations, but it just loves to lie and tell me I’m wrong

why is this?

Next console.log() a string in the following format: Dinner is served. We’re having [resolved value of the vegetablePromise ], [resolved value of the starchPromise ], [resolved value of the proteinPromise ], and [resolved value of the sidePromise ]. ie. ‘Dinner is served. We’re having broccoli, rice, chicken, and beans.’

There should not be a comma before ‘and’. This wasted an hour of my life, thanks.

6 Likes

The exercise recommends to use async/await sintax.

So an example would be using inline awaiting:

console.log(Dinner is served. We're having ${await variable}, ${await variable}, ${await variable}, and ${await variable}.);

1 Like

Yeah, I’ve been there, I copied the first string to avoid mistakes and the quote was different, and it was failing and I couldn’t know why until I saw it:

We’re having vs We’re having

2 Likes

When logging to the console within serveDinner can you use the string interpolation

${await vegetablePromise, await starchPromise, await . . . etc}

As opposed to having them in separate template literals, or do you have to do each independently?

It’s an oxford comma which is technically grammatically correct, but I agree it’s annoying to not be able to progress due to this.

5 Likes

So after trying both inline await and resolving the variables before logging them (and still not being able to pass the exercise), I saw Juanipsf’s comment above.

If you’re just copying in the string and then changing the content from [resolved value of the vegetablePromise ] to ${await var}, please make sure to change the apostrophe in we’re. It doesn’t recognise the copied apostrophe.

2 Likes

I don’t think so - You can put multiple variables inside a single set of braces if you were doing something like

You have bought ${numberOfApples} apples at ${cost} dollars each, bringing your total to ${numberOfApples * cost}

but I don’t think they can interpolate multiple variables.

I’m getting an error, seemingly from console.log line. I’ve checked and rechecked it, finally copied and pasted the line from the Hint (just in case, it was identical):

console.log(Dinner is served. We're having ${await vegetablePromise}, ${await starchPromise}, ${await proteinPromise}, and ${await sidePromise}.);

I get “Syntax error: missing } in template expression”

1 Like

And I do have the backticks around the console.log line, they are being deleted from comment here.