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

Jesus Olaiz
Jesus Olaiz
8,259 Points

Im confused on how the callbacks are working here

  const myPromise = new Promise( (resolve, reject) => {
    setTimeout(() => {
        resolve(7);
    }, 1000);
});


console.log(myPromise);

myPromise
    .then(a => a+5 )
    .then(b => b*6)
    .then( val => console.log(`You got ${val}`))

I know how to make it work, but there is something about the logic that has been just making me go kind of crazy.

myPromise
    .then(a => a+5 )

Written out in a pyramid of doom I can still follow the code pretty well and make sense of how the function is getting the result of the next, but here I just cant make sense as to how the engine knows that the result applies to the variable or parameter a. Like I said, I can make a promise work but I would love a bit more in depth explanation of what is happening here so I don't feel like I'm just guessing the whole time and some how getting lucky.

Thanks in advance!

1 Answer

Steven Parker
Steven Parker
216,863 Points

What you're seeing is just the nature of .then. The callback function you pass to it has one argument, the fulfillment value. When the callback is invoked, then passes that value to the function.

You might want to take a look at the MDN page on .then.