0

Consider the following:

const promise = new Promise((resolve) => {
    const res = 31 + 42;
    resolve(res);
})

promise.then( res => console.log(res));

I understand that as soon as resolve callback is called, this callback is pushed to the event queue to be executed later on Javascript's call stack.

But what about this line:

const res = 31 + 42;

Where is this line executed? It can't happen synchronously when the Promise is being created because control returns back immediately without executing the Promise constructor (which is later executed and might do some heavy calculations). It can't happen even in the Web API environment because the Web API environment doesn't have a javascript engine to execute js code. So, where is this line executed?

Daud
  • 7,429
  • 18
  • 68
  • 115
  • 3
    I think you should better correct your example, as it's not valid JS. – VLAZ Aug 14 '21 at 15:29
  • 1
    "*It can't happen synchronously when the Promise is being created because control returns back immediately without executing the Promise constructor (which is later executed and might do some heavy calculations)*" what makes you say that? – VLAZ Aug 14 '21 at 15:30
  • 1
    _"without executing the Promise constructor (which is later executed)"_, `new Promise` is the constructor being called it isnt executed at a later time. And the function passed to the constructor is executed from within the constructor itself. All top level code in the callback is run and execution returns to the constructor. – Patrick Evans Aug 14 '21 at 15:30
  • The script snippet you quoted -- `const promise = new Promise(resolve) { const res = 31 + 42; resolve(res); }` contains a syntax error. The syntactically correct statement would read `const promise = new Promise(resolve => { const res = 31 + 42; resolve(res); });` -- do you understand the difference? – Armen Michaeli Aug 14 '21 at 15:31
  • The `Promise` constructor takes a function as input. also, any ..not valid JS ( any object created with new keyword doesn't have a body like that of a function ) – D.B.K Aug 14 '21 at 15:31
  • I've corrected the syntax error. If the constructor function is immediately executed, then if we do some very long calculations inside the constructor function (in order to calculate the resolved value), then all that happens in the same thread in a blocking manner? At which point does the code start executing asynchronously in the Web API environment? – Daud Aug 14 '21 at 16:04
  • 1
    "*then if we do some very long calculations inside the constructor function (in order to calculate the resolved value), then all that happens in the same thread in a blocking manner?*" correct. "*At which point does the code start executing asynchronously in the Web API environment?*" no, the executor function given to the promise constructor runs synchronously. It's neither executed "somewhere else" nor is it running "at a different time". If all you have is blocking code, then the promise *won't be created* until that blocking code finishes. – VLAZ Aug 14 '21 at 16:16
  • @VLAZ Then, does the asynchronous part inside the executor function start only when a pre-defined async Web API like fetch or timeout is called, or when resolve or reject callbacks are called? Can't we write our own block of code that gets executed in a parallel thread? – Daud Aug 14 '21 at 16:19
  • 2
    A promise is **not** a way to use multithreading. It isn't "asynchronous", either. Promises are just a marker for some sort of operation. If you write a code that blocks the thread for 5 seconds and put it in a promise executor, it won't magically unblock the thread. If you want parallel processing, you can use a web worker, for example. Parallel execution in JS is an opt-in, using a promise doesn't automatically transform sync code to async. – VLAZ Aug 14 '21 at 16:23
  • 2
    @Daud Yes, only `fetch` and `setTimeout` are really asynchronous themselves, doing stuff in the background. Promises are ([like callbacks](https://stackoverflow.com/a/22562045/1048572)) just a notification mechanism. – Bergi Aug 14 '21 at 16:35

0 Answers0