Home > front end >  Where is resolved, and rejected passed from in promises (JavaScript)?
Where is resolved, and rejected passed from in promises (JavaScript)?

Time:12-22

Currently learning about promises, and I was trying to build a good connection and understanding of Promises in Asynchronous JavaScript. But I have become very curious about "Resolved", and "Reject" (I know that they are just variable name).

const promiseTest = new Promise((Resolved,Rejected) => {
  Resolved("Success");
})

Well, I couldn't really figure out much while surfing through the internet on solutions, what I do know is that Resolved, and Rejected are passed in values, from where I am not sure, I assumed the Promise class at first, but it seems that is not it, and through research I found an article from freecodecamp mentioning that the JavaScript language sends it or so, a more detailed but simplified response would be appreciated it.

CodePudding user response:

The promise constructor is going to make those functions, and then pass them to you. The functions are hidden from most of the world, but are exposed to you and only you, by passing them into your function

Here's some pseudocode of what it's doing:

class Promise {
  constructor(yourFunction) {
    const resolve = () => {
      // some code which knows how to resolve the promise
    }
    const reject = () => {
      // some code which knows how to reject the promise
    }

    yourFunction(resolve, reject);
  }
}
  • Related