0

When we create a Promise, the executor function is called immediately but the resolve callback is the one which gets executed asynchronously. This means that if I do some heavy work inside the executor function resulting in a result that gets passed to the resolve callback, then all that work gets executed in the same thread in a blocking manner, and only the result gets passed to us asynchronously via the callback. I hope this understanding is correct.

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
  • 7,429
  • 18
  • 68
  • 115
  • 1
    _"...but the resolve callback is the one which gets executed asynchronously. "_ - it is _invoked_ asynchronously but once it starts executing, no other code executes until the callback has finished executing. – Yousaf Aug 14 '21 at 17:49
  • 1
    Related: [javascript promises, the event loop, and the job queue](/q/55676922/4642212). – Sebastian Simon Aug 14 '21 at 17:51
  • Daud, I strongly recommend you read the material linked by @SebastianSimon. It may not give you a complete and clear picture of how these things work, but it should get you started with some minimal investment; you can also without a doubt find a lot of very good articles elsewhere. Without basic understanding of how ECMAScript _mandates_ scripts are executed (meaning, unlike some other more liberal programming language definitions, many of the things you inquire about are actually "set in stone"), you will be running the risk of writing potentially faulty applications. – Armen Michaeli Aug 14 '21 at 18:20

2 Answers2

1

The resolve callback is the one which gets executed asynchronously.

You might have meant the right thing but the phrasing is ambiguous.

No, resolve() can be called synchronously or asynchronously, depending on what the executor is doing with it.

new Promise(resolve => {
    resolve(); // all synchronous
});
new Promise(resolve => {
    setTimeout(resolve, 1000); // called later
});

The resolve() call immediately returns, but it schedules registered promise handlers if there were any.

The result gets passed to us asynchronously via the callback

Yes. A callback function that is passed to .then() is always executed asynchronously, to give consistent output order without releasing zalgo:

somePromise.then(() => console.log("second"));
console.log("first"); // no matter if `somePromise` is already fulfilled or not

Does the asynchronous part inside the executor function start only when a pre-defined async Web API like fetch or timeout is called.

Yes.

Can't we write our own block of code that gets executed in a parallel thread?

Indeed, we cannot do that. Some environments allow creating workers for parallel execution, but it's not a "block of code" - it's a separate worker that you have to communicate with through events.

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

When we create a Promise, the executor function is called immediately but the resolve callback is the one which gets executed asynchronously. This means that if I do some heavy work inside the executor function resulting in a result that gets passed to the resolve callback, then all that work gets executed in the same thread in a blocking manner, and only the result gets passed to us asynchronously via the callback. I hope this understanding is correct.

Yes

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?

Yes (but more, below)

Can't we write our own block of code that gets executed in a parallel thread?

Promises are not meant to implement multithreading. That can be achieved using Web Workers. Promises are just meant to invoke a callback function at some later moment in time. Preceding a particular invocation to callback, there might be pre-defined Web API functions (like setTimeout or fetch) which execute in a different thread, but that's not necessary in a Promise.

Remember that "asynchronous" does not mean "in another thread"

PS: This is my (possibly flawed) understanding of the topic. Feel free to write a new answer in case mine is wrong in any way.

Daud
  • 7,429
  • 18
  • 68
  • 115
  • _"Promises are just meant to invoke a callback function at some later moment in time"_ - IMHO, a more accurate description of a promise is: _it is a mechanism which notifies about the successful completion or a failure of an asynchronous operation_. – Yousaf Aug 14 '21 at 17:52