-1

Whenever we create a new promise class, we would do something like this:

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve("success");
  }, 4000)
});
promise.then((result) => console.log(result));

So, this is my way of thinking about how things work behind:

  1. Promise will have a constructor of an arrow function.
  2. setTimeout will run the resolve('success') function within the specified time limit, and returning a Promise with resolved state (which I think is another class because it has constructor)
  3. The then() function will run whatever function passed on the first step (So in this case, the anonymous arrow function that returns the Promise)

First of all, why we can pass a resolve and use it as a function in setTimeout? I thought we haven't even declared it yet and it's not the part of the window.

Second, can someone correct my way of thinking here (so maybe can help me explaining what is going on within the process)?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1. No, the constructor is being _passed_ an arrow function. 2. No, `setTimeout` doesn't return a promise (and the promise that `new Promise` creates is initially pending, not resolved. 3. No, it runs whatever function is passed to it, in your case the one that logs the result. And `resolve` _is_ declared, as a parameter. I'd recommend reading e.g. MDN's docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – jonrsharpe Jun 06 '21 at 14:59
  • resolve is the first argument of the new Promise callback. Change it to `new Promise((res,rej) => setTimeout(res, 4000, 'success'))` and it will work the same – charlietfl Jun 06 '21 at 14:59
  • `resolve` is part of the [Promise API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve). You're not passing it in or declaring it. – Andy Jun 06 '21 at 15:01
  • Possible duplicate of [When or who does pass resolve and reject functions to JS promises?](https://stackoverflow.com/q/40046657/1048572) or [`resolve` and `reject` functions inside a Promise's executor](https://stackoverflow.com/q/50413583/1048572)? – Bergi Jun 06 '21 at 15:07
  • Are you asking for a reusable function that when given a value and a time in milliseconds will resolve that value after that time, so that you don't have to keep repeating the same code? –  Jun 07 '21 at 14:27

1 Answers1

1

Promise will have a constructor of an arrow function.

Not sure what you mean there.

setTimeout will run the resolve('success') function within the specified time limit, and returning a Promise with resolved state

No. Neither setTimeout nor resolve nor the arrow function that you passed to new Promise do return a promise.

The then() function will run whatever function passed on the first step

No. The function passed to new Promise (know as the executor) is called immediately. Not from the .then() call. Notice that it's called even if you don't call .then(), or do it later only, or do it multiple times.

why we can pass a resolve and use it as a function? I thought we haven't even declared it yet and it's not the part of the window.

The new Promise constructor creates the resolve and the reject function. They do nothing but change the internal state of the promise, and schedule handlers that were registered on the promise object.

You might want to have a look at this example implementation or How is a promise/defer library implemented?.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375