109

Suppose I have the following code constructing a Promise:

function doSomethingAsynchronous() {
  return new Promise((resolve) => {
    const result = doSomeWork();

    setTimeout(() => {
      resolve(result);
   }), 100);
  });
}

At which point in time is doSomeWork() called? Is it immediately after or as the Promise is constructed? If not, is there something additional I need to do explicitly to make sure the callback is run?

user3840170
  • 26,597
  • 4
  • 30
  • 62
Kevin
  • 14,655
  • 24
  • 74
  • 124
  • 4
    [ECMAScript, 25.4.3.1 `Promise(executor)`](http://www.ecma-international.org/ecma-262/6.0/#sec-promise-executor) -> Step 9 – Andreas Feb 08 '17 at 16:49
  • It doesn't matter: if you need to enforce order of operations, do so explicitly. Different implementations will handle promises with slight differences in behavior (bluebird vs native map, for example). – ssube Feb 08 '17 at 16:52
  • @guest271314 I'm writing some tests and need to mock a function that returns a Promise. I want the Promises the mock returns to resolve immediately so that the tests run successfully, so I just wanted to double-check that there wasn't some kind of magic I had to invoke to get them to run. – Kevin Feb 08 '17 at 17:21
  • That, and I want to understand how Promises work under the hood. – Kevin Feb 08 '17 at 17:21
  • @Kevin Why are you adding this question with *"If not"* ? Did you see my answer ? It is **guaranteed** the executor is always immediately executed even before the Promise constructor returns. There's no "implementation details" here, it's specified. – Denys Séguret Feb 08 '17 at 17:26
  • @DenysSéguret, yep, I saw your answer (and I'm likely to accept it). I added the third question to add some more context for why I was asking the question for future readers, but I don't need any more clarification myself. – Kevin Feb 08 '17 at 17:27
  • 2
    This may help explain things: http://stackoverflow.com/questions/42031051/resolve-order-of-promises-within-promises/42043784#42043784. It covers a slightly more complicated situation (a promise within a promise), but definitely covers what is going on here, including what happens when a promise resolves before the `.then()` handlers are attached. – jfriend00 Feb 08 '17 at 17:41
  • 1
    Please don't close this question as a duplicate of one which only has bad answers. And *no*, this isn't implementation dependent but specified. – Denys Séguret Sep 14 '18 at 15:09
  • 1
    possible duplicate of [Is the Promise constructor callback executed asynchronously?](https://stackoverflow.com/q/29963129/1048572) – Bergi Jul 27 '19 at 16:42

4 Answers4

99

Immediately, yes, by specification.

From the MDN:

The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object)

This is defined in the ECMAScript specification (of course, it's harder to read...) here (Step 9 as of this edit, showing that the executor is called synchronously):

  1. Let completion be Completion(Call(executor, undefined, « resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]] »)).

(my emphasis)

This guarantee may be important, for example when you're preparing several promises you then pass to all or race, or when your executors have synchronous side effects.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 48
    _Immediately_ is confusing. The executor is really called **synchronously** by the Promise constructor. – Paleo Sep 28 '17 at 15:11
  • 1
    It seems that MDN and the ES spec doesn't say that anymore, but I think you can still assume it runs sync since the steps 1-11 mentions return a promise, which you do get back synchronously. It doesn't mention running it async or queueing it somewhere like how [PerformPromiseThen](https://262.ecma-international.org/6.0/#sec-performpromisethen) says "Append fulfillReaction as the last element of the...". – dosentmatter Jan 26 '21 at 06:56
  • One addition: it will be executed instantly (synchronously), but `then()` will call its callback in the next tick (asynchronously). – OZ_ Nov 25 '22 at 23:49
  • yes, Executive function block is executed synchronously. But the resolve and reject function call back is depended on then() and catch() calls. So then() is calling resolve and reject funs call back asynchronously, i.e making them wait in micro task queue and executing them when the class stack is empty. so when then() is calling the resolve and rreject asynsly what is the use of again making the executive func block async by wrapping it in the setTimeout() ? – Kondaveti Charan May 18 '23 at 04:14
22

You can see from below the body is executed immediately just by putting synchronous code in the body rather than asynchronous:

function doSomethingAsynchronous() {
  return new Promise((resolve) => {
    console.log("a");
    resolve("promise result");
  });
}
doSomethingAsynchronous();
console.log("b");

The result shows the promise body is executed immediately (before 'b' is printed).

The result of the Promise is retained, to be released to a 'then' call for example:

function doSomethingAsynchronous() {
  return new Promise((resolve) => {
    console.log("a");
    resolve("promise result");
  });
}

doSomethingAsynchronous().then(function(pr) {
  console.log("c:" + pr);
});
console.log("b");

Result:

a
b
c:promise result

Same deal with asynchronous code in the body except the indeterminate delay before the promise is fulfilled and 'then' can be called (point c). So a and b would be printed as soon as doSomethingAsynchronous() returns but c appears only when the promise is fulfilled ('resolve' is called).

What looks odd on the surface once the call to then is added, is that b is printed before c even when everything is synchronous.

Surely a would print, then c and finally b?

The reason why a, b and c are printed in that order is because no matter whether code in the body is async or sync, the then method is always called asynchronously by the Promise.

In my mind, I imagine the then method being invoked by something like setTimeout(()=>{then(pr)},0) in the Promise once resolve is called. I.e. the current execution path must complete before the function passed to then will be executed.

Not obvious from the Promise specification why it does this?

My guess is it ensures consistent behavior regarding when then is called (always after current execution thread finishes) which is presumably to allow multiple Promises to be stacked/chained together before kicking off all the then calls in succession.

Teocci
  • 7,189
  • 1
  • 50
  • 48
Moika Turns
  • 677
  • 11
  • 17
19

Yes, when you construct a Promise the first parameter gets executed immediately.

In general, you wouldn't really use a promise in the way you did, as with your current implementation, it would still be synchronous.

You would rather implement it with a timeout, or call the resolve function as part of an ajax callback

function doSomethingAsynchronous() {
  return new Promise((resolve) => {
    setTimeout(function() {
      const result = doSomeWork();
      resolve(result);
    }, 0);
  });
}

The setTimeout method would then call the function at the next possible moment the event queue is free

asdfg_rocks
  • 117
  • 1
  • 11
Icepickle
  • 12,689
  • 3
  • 34
  • 48
5

From the EcmaScript specification

The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object)

Consider the following code:

let asyncTaskCompleted = true

const executorFunction = (resolve, reject) => {
  console.log("This line will be printed as soon as we declare the promise");
  if (asyncTaskCompleted) {
    resolve("Pass resolved Value here");
  } else {
    reject("Pass reject reason here");
  }

}

const myPromise = new Promise(executorFunction)

When we execute the above code, executorFunction will be called automatically as soon as we declare the Promise, without us having to explicitly invoke it.

Teocci
  • 7,189
  • 1
  • 50
  • 48
Himansh
  • 879
  • 9
  • 15