Complete a Promise

Tell us what’s happening:

Your code so far


const makeServerRequest = new Promise((resolve, reject) => {
// responseFromServer represents a response from a server
let responseFromServer;
  
if(responseFromServer) {
   resolve("We got the data");  // change this line
} else {  
   reject("Data not received"); // change this line
}
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0.

Challenge: Complete a Promise with resolve and reject

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/es6/complete-a-promise-with-resolve-and-reject

I don’t understand, reject and resolve are functions ???
I got resolve this problem, but a don’t understand this pass
( reject, resolve) =>

The arguments are functions?

Functions can be values in JavaScript. A function is in essence a callable object.

1 Like

This is definitely look like a function. If you want to reassure yourself you can additionally try this check in the body of the function you pass to the Promise constructor:

console.log(typeof resolve, typeof reject);

So, to summarize:

typeof Promise; // function (constructor function aka class)
Promise.length; // 1 (Promise connstructor expects 1 argument - callback function)
typeof resolve; // function
typeof reject; // function
// Callback expect 2 arguments and both - again functions
// If you call resolve() - promise resolves
// If you call reject() - promise rejects
1 Like

You can read more about the Promise object and its methods. You can also check out the specs for the executor function and the resolve/reject function arguments.

1 Like