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 trialMoe Ghashim
11,109 PointsHow was nextValue assigned a number?
I have two questions: 1- When I run my code in the workspace, nothing comes out. Here is my code:
var calculationPromise = new Promise (function (resolve, reject) {
setTimeout(function(){
resolve (1 + 1);
}, 1000);
});
calculationPromise.then(function (value) {
return value + 2;
}).then(function (nextValue) {
console.log("The final value is", nextValue);
});
2- When I watched the video, I didn't understand how nextValue
was assigned with the result number 4
.
2 Answers
Mary Paul
13,792 PointsHey Moe,
So, your code works in the console. Did you have your console opens when you ran your code? It worked fine when I tried it out.
NextValue is just the name we give to the parameter that function is receiving. The important thing about promises is that we are requesting that the functions be done in order (asynchronously) rather than all at the same time (synchronously.) We are waiting for one thing to finish before we do the next thing.
That means in this case we start with a var calculation Promise = 1+1 (which is 2, of course,)
Then we pass that number as the new parameter to calculationPromise.then and we return that value +2 (so that's now 4)
Then we pass that number along again to the second .then as the new parameter (nextValue) which interpolates it into our console.log sentence. Here we could call the parameters anything we like as long as we are consistent.
Does that make sense?
Amber Clark
10,001 PointsHow does the anonymous function in the promise constructor return a value when it doesn't explicitly return anything?