Understanding callback parameters of JS Promises

I’m just finishing up the ES6 section of the JavaScript lessons and I’m getting a little confused in the Promises lessons.

I don’t understand where the “result” and “error” parameters are coming from in the code below. Are these pre-defined functions of ES6? Are they something you can only call from promises?


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

makeServerRequest.catch(error => {
  console.log(error);
});

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:97.0) Gecko/20100101 Firefox/97.0

Challenge: Handle a Rejected Promise with catch

Link to the challenge:

Hello! Welcome to the community :partying_face:!

The result and error parameters come from the resolve and reject function call, respectively.

We got the data would be the content of the result variable, while Data not received would be the content of the error variable. In fact, since in this case you’re defining the promise and how it resolves or gets rejected, you could change the text to whatever you like and it would get received by the then or error callbacks:

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("Whatever you put here will be handed to the 'then' callback");
} else {  
  reject("Whatever you put here will be handed to the 'catch' callback");
}
});

makeServerRequest.then(result => { // This is the resolve("...")
console.log(result);
});

makeServerRequest.catch(error => { // This is the reject("...")
  console.log(error);
});

Does it help?

Hello, thanks for the welcome & reply! This makes more sense now.

1 Like

When you say new Promise(someCallback), you use the Promise constructor(its alreayd defined by JS), which creates this promise object, based on how you designed the callback you pass ot it(i named it “someCallback”). By default, you want this callback to accept two arguments, which are the resolve and reject functions(you can name the functions as you like), which are necessary for you to utilize, in case the promise has resolved successfully, or it failed(was rejected). Those two functions are passed to the Promise object on its creation. The promise object you defined has two core methods, then and catch, which you attach to it, to handle when the promise is resolved, or rejected respectively. If the promise is resolved, the resolve function is called, and whatever value you passed to it(in the case “We got the data”) is passed in the then method as an argument. In the example code, you have access to it under the name “result”. If the promise enters rejected state, the reject function is called instead and you usually pass an error message to it, which comes out in the catch method, obviously you want to name the parameter accordingly, do describe what it represents(an error). You can ultimately say myPromise.then(handleResolve).catch(handleReject) and in the handleResolve/handleReject callbacks, to deal with the respective case.
Its a hard concept to wrap your head around and its not one you are likely to encounter so early in your coding education(unless you had other courses before), so just try to get as much as you can and leave it as work in progress for later time, when you need to implement it in your projects.

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