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 Understanding Promises Create a Promise

breakfastPromise.then(val => console.log(val) ) Is this a shorthand version of a function inside a method?

So Traditional Funtion name ( ) { functionbody }
On one line looks like: name ( ) { function body }

AnonymousFunction: // will need to be appended to a variable or 'something' in order to call it. ( ) { functionbody }
On one line looks like: ( ) { function body } is the same as ( ) => { function body } an anonymous function that takes no parameters:

an anonymous function that takes two or more parameters: (param 1, param 2) { functionbody} is the same as (param 1, param 2) => {functionbody}

Here's where I lose it - an anonymous function that takes one parameter ( 1 param) { function body } is the same as..... myparam => { function body }

so, where does 'myparam' come from and what is it? A parameter masquerading as a variable?

Then we take a step farther and get: breakfastPromise.then (val => console.log (val) ) .then is a method that can take one or two parameters. The second param is optional. The first param is automatically handled as 'resolve'. Why is there a function inside the then method? Where are the ( ) for the parameters and where are the { } for the function body? Can someone write the breakfastPromise.then in long hand? Why couldn't one just write breakfastPromise.then(console.log(resolve)); ?

Reggie Williams
Reggie Williams
Treehouse Teacher

Hey Kylie Soderblom in myparam is still a parameter of the function and just a placeholder in this situation. When written on one line it can just be done without parentheses. The function inside the then is run after the promise. Like the ; there are times when () and {} are recommended but not required in javascript

Thank you. For a seasoned coder, it most likely is second nature to know when one can skip () {}. But I'm still trying to make sense of when one is allowed to short cut. 1) Could you give me a long-hand example of myparam => { function body}?
Is it the same as let placeholder = (myparam) {function body} ? 2) And then why can't one shortcut and write breakfastPromise.then( console.log("whatever") ); I don't understand where the val comes from? Is it a placeholder or does it contain a value? I'm still struggling to read the arrow function correctly.

1 Answer

Reggie Williams
STAFF
Reggie Williams
Treehouse Teacher

Hey Kylie Soderblom asking questions and seeking understanding as you are is how one becomes seasoned! myparam => { function body} is the same as let placeholder = (myparam) {function body} except that in the second option we can reuse the function because it is stored in the placeholder variable.

The promise returns a value when it is fulfilled. That's where val comes in but it must be passed into the function in then so it is in the correct scope.