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 trialJesus Olaiz
9,210 PointsIm 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
231,248 PointsWhat 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.