ES6 - Handle a Fulfilled Promise with then

Tell us what’s happening:

Describe your issue in detail here.

Your code so far

const makeServerRequest = new Promise((resolve, reject) => {
  // responseFromServer is set to false to represent an unsuccessful response from a server
  let responseFromServer = false;
    
  if(responseFromServer) {
    resolve("We got the data");
  } else {  
    reject("Data not received");
  }
});

makeServerRequest.then(result => {
  console.log(result);
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36 Edg/118.0.2088.76

Challenge Information:

ES6 - Handle a Rejected Promise with catch

Hi,
result was never defined before. How does the function log the resolve value when I output result? What is the magic behind that?

Best,

You are basically naming the argument to the function that the “.then” method takes as “result”. When you say the following:

result => {
  console.log(result);
}

…you are actually defining an anonymous function which takes result as an argument. For example, you could instead replace the above lines with the following:

function(result) {
  console.log(result);
}

By the way, you don’t have to name the argument “result”, you could give it any other name you want, such as “result1” or “bob” or whatever you like and then console log the same in the function itself. I hope that helps. Let me know if you need further clarification. I found the following page to be helpful for this method: .then method on MDN

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.