0

I am new to programming and JavaScript and I don’t understand those “ resolve “and “ reject” arguments of the code below.

const myPromise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('foo');
  }, 300);
});

myPromise
  .then(handleResolvedA, handleRejectedA)
  .then(handleResolvedB, handleRejectedB)
  .then(handleResolvedC, handleRejectedC);

Tutorials say Promise class takes two arguments which are functions. But where are resolve and reject functions ??? If it is written in this promise class , I think those functions will be written like myPromise.resolve ??

I also wanna know if promise class always have to take resolve and reject as arguments ? It can’t be resolve1 and resolve2 ?? Those names are registered or something???

resolve and reject come from nowhere and I am confused. I hope someone help me ;( thanks

  • It is part of the api... https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – epascarello Nov 23 '21 at 03:32
  • 2
    "*Tutorials say Promise class takes two arguments which are functions.*" - no. The `new Promise` constructor takes *one* argument, a function ("executor") that will be called with two functions as arguments. And yes, you can name the parameters of that executor function whatever you want. – Bergi Nov 23 '21 at 03:36
  • 1
    _"have to take resolve and reject as arguments"_... the argument names can be anything you want `new Promise((fred, barney) => { ... })` – Phil Nov 23 '21 at 03:36
  • https://stackoverflow.com/questions/54603225/what-values-are-passed-to-the-executor-during-the-promise-object-creation https://stackoverflow.com/questions/61442578/how-javascript-promises-work-behind-the-scenes – Bergi Nov 23 '21 at 03:40
  • You are defining a function with 2 arguments (`resolve` and `reject`) and the JavaScript will call that function. It can be named anything. – MrMythical Nov 23 '21 at 03:42

0 Answers0