Janelle Mackenzie7,510 Points
Purpose of the 'val' and 'err' functions?
What is the purpose of the 'val' and 'err' functions? I don't understand how/why those were created..
Robert ManolisTreehouse Guest Teacher
Hi Janelle, those are actually anonymous functions. The err and val are just parameters that are being passed to anonymous functions. I'll try to talk you through what's happening here.
So when Guil calls breakfastPromise, a "promise" is returned. If you chain a .then or .catch method to the promise, then the value of the returned promise gets passed to those .then or .catch methods. The .then and .catch methods accept an anonymous callback function as an argument. Those callbacks let you perform some operations on whatever value is passed to them from the promise. But you need a way to reference that value being passed, which is accomplished with whatever parameter you pass to the anonymous callback function that was passed to the .then or .catch methods.
I know that probably sounds really confusing. Think about it like this, you can change the names of val and err to anything you want in those callback functions, and the actual value they represent will not be affected, and will still get logged to the console. Give it a try.
Hope that helps shed some light on this.