Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Asynchronous Programming with JavaScript Asynchronous JavaScript with Callbacks Callbacks Review

Callbacks makeMeal quiz question

I hate asking for answers to quiz questions but this one has been bugging me for quite some time, and I cannot work out the answer from reading over the callback review instructions.

The quiz at the end of this section asks you to "Set the makeMeal function to invoke the callback function passed to it":

function makeMeal(meal, cb) {
  alert(`Preparing ${meal}.`);
  \\insert cb code here ;
}

makeMeal('lunch', () => {
  alert('Finished the meal!');
}); 

looking over the callback review instructions it would seem that the 'preparing meal' alert needs to pop up first, and then the 'finished meal' alert pops up last, but when I try to invoke the cb function noted above by typing things such as

cb(makeMeal());

or

cb(lunch);

it doesn't work?

The question seems simple but I am having trouble understanding the basic implimentation, I would appreciate it if someone could give me some hints!

Thank you in advance.

7 Answers

It is a part of makeMeal

in the makeMeal function there is (meal, cb). So if you need to invoke the function for cb, you need to enter:

return cb();

Thank you Edgardo

It turns out I overthought this question way too much as it is much simpler than I initially thought. I won't give it away but the key thing to concentrate on after reading :

set the makeMeal function to invoke the callback function passed to it

is the word 'invoke'.

I'll leave this question up incase someone else gets stuck:)

i am stuck on this. What is the resolution?

return cb();

I know it is not recommended to give the answer but sometimes I've been stuck myself and seeing the answer clear it all up.

I'm stuck someone to help

Michelle Mashamba , as I've come to learn, the vocabulary used by the instructors is important to pick up on. So for this test you have to:

"Set the makeMeal function to invoke the callback function passed to it"

so you have function makeMeal with two parameters passed into it (meal, cb)

the first line in the function is an alert which takes in the parameter meal, which when invoking the function, would be what is passed in for meal for example makeMeal(lunch, cb)

so to invoke makeMeal you would need to end the function using the parameter cb

ddeveloper , it took me a bit but if you remember the code of the previous videos, you will see an example of this.

So it is asking you to "invoke the callback function."

That being said:

function makeMeal(meal, cb) { alert(Preparing ${meal}.); \ here you need to put the code which would lead to the function below }

makeMeal('lunch', () => { alert('Finished the meal!'); });

If makeMeal(meal) is makeMeal(lunch) that means makeMeal(cb) is makeMeal(function)

So what needs to be the answer?

Edgardo Serafin

it should be makeMeal

Also helped me. Thanks you guys