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 () 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 () below!
Agree with a comment or answer? Like () to up-vote the contribution!
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 .”
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
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?
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.
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?
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)
})
}
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!
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.
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:
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 thevegetablePromise ] to ${await var}, please make sure to change the apostrophe in we’re. It doesn’t recognise the copied apostrophe.
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”