Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Edgardo Serafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Edgardo Serafin
Full Stack JavaScript Techdegree Student 11,879 Points

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'm stuck someone to help

Edgardo Serafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Edgardo Serafin
Full Stack JavaScript Techdegree Student 11,879 Points

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

Edgardo Serafin
seal-mask
.a{fill-rule:evenodd;}techdegree
Edgardo Serafin
Full Stack JavaScript Techdegree Student 11,879 Points

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?

Hugo Magalhaes
Hugo Magalhaes
6,064 Points

Also helped me. Thanks you guys