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
Travis Fulton
13,620 PointsI don't understand how or why 'value' is working here
var calcPromise = new Promise(function(resolve, reject){
setTimeout(function(){
resolve(1 + 1);
}, 1000);
});
calcPromise.then(function(value){
console.log("the answer is ", value);
});
It seems like 'value' should have been declared somewhere, but it seems like it's magically just returning the result
3 Answers
Steven Parker
243,656 PointsYou are declaring "value" as the name of the parameter used by your then function:
calcPromise.then(function(value) {
The promise passes the actual value when it calls that function.
Travis Fulton
13,620 PointsI meant I'd expect 'value' to have been declared as a variable for it to work like this. I see that it's declared as a parameter, but I don't see how it 'knows' that it's assigned to the result of the math equation in the setTimeout. Thanks.
Steven Parker
243,656 PointsThe "value" parameter is passed to the function you registered with then when your promise calls the "resolve" function and passes the math result to it as the argument.
Travis Fulton
13,620 PointsAh! Thanks.