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
Victor Mercier
14,667 PointsDo not really undertsand multiple then (chaining)
function addFive(n) {
return n + 5;
}
function double(n) {
return n * 2;
}
function finalValue(nextValue) {
console.log(`The final value is ${nextValue}`);
}
const mathPromise = new Promise( (resolve, reject) => {
setTimeout( () => {
// resolve promise if 'value' is a number; otherwise, reject it
if (typeof value === 'number') {
resolve(value);
} else {
reject('You must specify a number as the value.')
}
}, 1000);
});
const value = 5;
mathPromise
.then(addFive)
.then(double)
.then(finalValue)
.catch( err => console.log(err) )
2 Answers
Steven Parker
243,266 PointsThe concept of "chaining" is simply calling a method on the result of another method. The code you show here has a great example in the last few lines.
You show the "then" method being called on "mathPromise", and another "then" chained onto it, followed by yet another "then" chained onto that, and finally a "catch" chained on at the end. Each method is called in order, using the result from the previous one. Note that for this to work, all methods must return an object of the same class as what the next method is defined on.
Did you have any more specific question about chaining?
Justin Horner
Treehouse Guest TeacherHello, Victor 👋
Steven has already posted a great answer here, so I'll just add to it to say when you call the then function on a promise, it returns a new promise that resolves to the return value of the function you pass to it. This means a new promise is returned on each call to then, allowing you to chain calls together.
Here's a step-by-step breakdown:
-
mathPromiseresolves to 5 -
thenis called onmathPromiseand we pass the functionaddFive - The resolved value (5) from the previous promise is passed to
addFiveas thenparameter -
addFiveis called and returns 10 - A new promise is created and resolves to the return value of
addFive, which is 10. This new promise is returned to us from thethenfunction. - Since we have a new promise from the first
thencall, we can callthenagain.
This process is simply repeated for every chained function called. Each time, a function is passed that resolves to a value and passed to the next chained function.
You can find more information on Promises at the Mozilla Developer Network (MDN).
I hope this helps.
Victor Mercier
14,667 PointsVictor Mercier
14,667 PointsYour answer is good but what do you mean by the same class?
Steven Parker
243,266 PointsSteven Parker
243,266 PointsFor example, if you're calling a method on a Promise, it must return a Promise for you to be able to chain another of the same calls (which is what "then" does).