Skip to content
Advertisement

What is the relationship, if any, between the “executor” and the function passed to the then() method? [closed]

The “executor” function I am referring to:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise#parameters

And the then() method:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

Seems like there is some overlap between their meaning or purpose.

In both cases, we often name their parameters “resolve” and “reject” – (Edit: this is obviously false, I mixed things up somehow).

I would almost think the functions passed to then() are somehow passed on to the executor.

But that doesn’t seem to be the case:

  1. The documentation points out, that the functions passed to the executor are automatically generated:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise#parameters

At the time when the constructor generates the new Promise object, it also generates a corresponding pair of functions for resolutionFunc and rejectionFunc; these are “tethered” to the Promise object.

  1. And we can also settle the promise without ever calling the then() method.

To summarize:

I am thinking that in case of the executor, the resolve and reject functions are functions generated by the constructor, but we control calling them to settle the promise with a result.

The resolve and reject functions we write and pass to then() are automatically called with the same result as before, but after the promise is settled.

Is this correct?

Advertisement

Answer

There is no real connection at all between the two as they are completely different things for completely different purposes.

The executor function is what determines when the promise eventually resolves or rejects. The two arguments are functions you call when the promise should resolve (with an optional value) or should reject (with a reason).

The one or two arguments passed to .then() are listeners that get called when the promise changes state, either becomes resolved or becomes rejected. They don’t determine when the promise gets resolved or rejected, but are instead listeners to when that happens.

Promises (at the 10,000′ level) are just a monitoring system for asynchronous event completion or error. The executor determines when the promise completes and the .then() or .catch() handlers allow you to add listeners so you get notified when the promise becomes fulfilled or rejected.

I am thinking that in case of the executor, that the resolve and reject functions are functions generated by the constructor, but we control calling them to settle the promise with a result.

Yes, that is correct – they are generated in the constructor. You control when/if they are called.

The resolve and reject functions we write and pass to then() are automatically called with the same result as before, but after the promise is settled.

Think of these as onFulfilled and onRejected listener functions. Yes, the appropriate one is called when the promise is settled.


A few other points. You can have a promise with an executor with no .then() at all. For example, you might only have a .catch() because all you want to know about this operation is if it had an error or not. So, even without any onFulfilled listener, the promise executor works just fine (because it has nothing to do with the listeners).

And, you can have multiple .then() listeners on the same promise if you have multiple parts of your code interested in the outcome of this promise. You can even add a .then() listener after the promise is already fulfilled or rejected and it will get called on the next cycle of the event loop. I mention these just to further confirm that there’s no correlation between .then() listeners and the resolve/reject handlers you get from the promise executor.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement