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

JavaScript Asynchronous Programming with JavaScript Understanding Promises Create a Promise

Jasper Kop
Jasper Kop
10,423 Points

Why is the text from resolve displayed by the val function?

I don't get the link between the breakfastPromise.then (val=> console.log(val)) and the resolve text that shows up in the console.

I find the whole call-back and promise hard to understand.

3 Answers

Hi Jasper!

shahzaib rafiq is right on here. val is just a variable holding the resolve information. It may be easier to understand if you right it out like this:

const breakfastPromise = new Promise((resolve, reject) => {
    setTimeout(() => {
        resolve('Your order is ready!');
    }, 3000);
});

console.log(breakfastPromise);
breakfastPromise.then((resolve) => console.log(resolve));  // .then() is a method that uses the resolved information from the promise you've previously created

Hope this helps!

shahzaib rafiq
PLUS
shahzaib rafiq
Courses Plus Student 3,001 Points

for understanding take the resolve like a message variable which store the success message and .then() is the method which takes one parameter (you can name that parameter whatever you want) and performs the task on that parameter which you pass. Like .then( val => {console.log(val)} ) or .then(msg => {console.log(msg)}) it will print the value that store in resolve.

Alex Hort-Francis
Alex Hort-Francis
17,074 Points

This concept of 'automatically provided arguments' seems to explain this: https://gomakethings.com/automatically-provided-arguments-in-javascript-callback-functions/

"With callback functions on JavaScript methods, the arguments are automatically passed in by the method itself."