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 trialAgnieszka Niemiec
7,359 PointsWhere is nextValue parameter getting its value from?
How does it work, that the function finalValue knows the value of nextValue? I'm sorry if my question makes no sense. I'm having trouble understanding how does the program know which values to use as arguments...
3 Answers
teofanescucosmin
14,058 Pointsconst value = 5;
mathPromise
// the first function addFive recive as argument/value the variable value (in this example is 5).
.then(addFive) // 5 + 5
// the next function double that is called recive as argument/value the result of last function called. last function was addFive (function get 10 as argument)
.then(double) // 10 * 2
// the next function called is addFive that will recive the argument/value the result of doblue function (function get a 20 as argument)
.then(addFive) // 20 + 5
// the result of the secound addFive function is argument/value for the finalValue function (function recive 25 as argument/value )
.then(finalValue)
.catch( err => console.log(err) )
Steven Parker
231,261 PointsThe argument to a "then" is a function which takes a value argument. So when the "then" calls "finalValue", it passes it a value which will become the "nextValue" within that function.
For more details, see the MDN page on Promise.prototype.then().
Kyouhyung Kim
13,136 PointsDoes it mean that onFulfilled and onReject functions must always be declared with only zero or one argument?
Steven Parker
231,261 PointsThe "onFulfilled" function is passed a single argument which is the "fulfillment value". The "onRejected" function is passed a single argument which is the "rejection reason".
If you don't provide an "onRejected" function (as is the case in the above example), the system will just throw an error if a rejection occurs.
Marcin Lipski
9,947 PointsI found it hard to understand too, but looking at mdn then() specification helped me a lot. Here is the link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then
Happy .
11,414 PointsHappy .
11,414 Pointsbit of a necro-post, but i found this also confusing, but simple enough once you see it.
When the Promise resolves (value IS a number), it .then passes value into the chain of functions.