JavaScript Asynchronous Programming with JavaScript Understanding Promises Create a Promise

Jasper Kop
Jasper Kop
9,057 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.

2 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);

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
shahzaib rafiq
Courses Plus Student 2,999 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.