1

I am searching whether Promise.resolve(1); includes any mcirotask queue or not? There is no .then() or .catch() handler attached to it. Only Promise.resolve(1); Example:

const v = Promise.resolve(1);

According to ECMA 2024 specs, I have read following algos:

27.2.4.7 Promise.resolve ( x )

This function returns either a new promise resolved with the passed argument, or the argument itself if the argument is a promise produced by this constructor.

1. Let C be the this value.
2. If C is not an Object, throw a TypeError exception.
3. Return ? PromiseResolve(C, x).

Note This function expects its this value to be a constructor function that supports the parameter conventions of the Promise constructor.

27.2.4.7.1 PromiseResolve ( C, x )

The abstract operation PromiseResolve takes arguments C (a constructor) and x (an ECMAScript language value) and returns either a normal completion containing an ECMAScript language value or a throw completion. It returns a new promise resolved with x. It performs the following steps when called:

1. If IsPromise(x) is true, then
a. Let xConstructor be ? Get(x, "constructor").
b. If SameValue(xConstructor, C) is true, return x.
2. Let promiseCapability be ? NewPromiseCapability(C).
3. Perform ? Call(promiseCapability.[[Resolve]], undefined, « x »).
4. Return promiseCapability.[[Promise]].

27.2.1.3.2 Promise Resolve Functions

A promise resolve function is an anonymous built-in function that has [[Promise]] and [[AlreadyResolved]] internal slots.

When a promise resolve function is called with argument resolution, the following steps are taken:

1. Let F be the active function object.
2. Assert: F has a [[Promise]] internal slot whose value is an Object.
3. Let promise be F.[[Promise]].
4. Let alreadyResolved be F.[[AlreadyResolved]].
5. If alreadyResolved.[[Value]] is true, return undefined.
6. Set alreadyResolved.[[Value]] to true.
7. If SameValue(resolution, promise) is true, then
a. Let selfResolutionError be a newly created TypeError object.
b. Perform RejectPromise(promise, selfResolutionError).
c. Return undefined.
8. If resolution is not an Object, then
a. Perform FulfillPromise(promise, resolution).
b. Return undefined.
9. Let then be Completion(Get(resolution, "then")).
10. If then is an abrupt completion, then
a. Perform RejectPromise(promise, then.[[Value]]).
b. Return undefined.
11. Let thenAction be then.[[Value]].
12. If IsCallable(thenAction) is false, then
a. Perform FulfillPromise(promise, resolution).
b. Return undefined.
13. Let thenJobCallback be HostMakeJobCallback(thenAction).
14. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).
15. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
16. Return undefined.
The "length" property of a promise resolve function is 1𝔽.

see step 8.a

27.2.1.4 FulfillPromise ( promise, value )

The abstract operation FulfillPromise takes arguments promise (a Promise) and value (an ECMAScript language value) and returns unused. It performs the following steps when called:

1. Assert: The value of promise.[[PromiseState]] is pending.
2. Let reactions be promise.[[PromiseFulfillReactions]].
3. Set promise.[[PromiseResult]] to value.
4. Set promise.[[PromiseFulfillReactions]] to undefined.
5. Set promise.[[PromiseRejectReactions]] to undefined.
6. Set promise.[[PromiseState]] to fulfilled.
7. Perform TriggerPromiseReactions(reactions, value).
8. Return unused.

Am I right in saying that Promise.resolve(1) does not include any microtask?

23
  • 1
    IOW: Promise.resolve(1); does indeed not include any microtasks, it's wherever you have any then callbacks attached that it becomes relevant, and its when processing the microtask queue. Note it might also be worth pointing out the processing of the microtask queue is actually sync, and not async, even thought it feels async.. Commented Aug 28, 2024 at 14:26
  • 1
    @aLearner, it seems by quoting the specs you have answered your own question. Commented Aug 28, 2024 at 14:48
  • 1
    Sure, I understand your point. It just sounds so confusing that you have a different concept of asynchrony. Commented Aug 28, 2024 at 16:05
  • 1
    @trincot For me if something is async, its something that allows the main threads event loop to continue, this was the case for when I coded in C, and had to mess with PeekMessage / Postmessage etc. In Javascript you have things like fetch / setTimeout etc, that I believe are async, and don't block the main event loop. And is why I wanted to make the distinction that setTimeout(fn) is not going to work the same as Promise.resolve().then(). A better example -> shorturl.at/hnj6R Commented Aug 28, 2024 at 16:19
  • 1
    @Keith you're basically right, it is synchronous, but only if you take the event loop as reference. For the user's script (which matters for most of us here), then it's async. Note that there is no prioritization system at all though between microtasks and tasks. Just like you can have multiple callbacks fire in a single task for the same event, you can have many microtasks run in a single task, so your model is actually closer to the truth than theirs. Where even a user-blocking task would not block the UI for more than a few rendering frames, a microtasks loop will. Commented Sep 8, 2024 at 23:59

1 Answer 1

1

No, Promise.resolve(1) does not involve any microtasks. As you concluded correctly, it just returns a fulfilled promise: the promise is constructed and then resolved (in particular: immediately fulfilled) without any reactions to schedule, before Promise.resolve returns.

Sign up to request clarification or add additional context in comments.

4 Comments

27.2.1.4 FulfillPromise ( promise, value ) contains 7. Perform TriggerPromiseReactions(reactions, value). and in 27.2.1.8 TriggerPromiseReactions ( reactions, argument ) steps are 1. For each element reaction of reactions, do a. Let job be NewPromiseReactionJob(reaction, argument). b. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).. The reactions here will be undefined as concluded from 27.2.1.4 FulfillPromise ( promise, value ). SO what will be behaviour of 1. For each element reaction of reactions, do in TriggerPromiseReactions(reactions, value)?
No, it's not "undefined". It's initialised to an empty list in the constructor, and therefore the loop over that list simply does nothing.
But why it will go to 27.2.3.1 Promise ( executor )? I am using Promise.resolve(1) as stated in my initial problem so it should go to 27.2.4.7 Promise.resolve ( x ) according to which following statement is being executed 4. Set promise.[[PromiseFulfillReactions]] to undefined?
It goes into the promise constructor during NewPromiseCapability(C). Notice the promise.[[PromiseFulfillReactions]] = undefined happens only after "Let reactions be promise.[[PromiseFulfillReactions]]" which is the empty list that's being passed to TriggerPromiseReactions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.