My objective is to compare the behavior of a promise's resolve
function with the corresponding process outlined in the ECMAScript specification. Specifically, I am interested in understanding how the resolve function behaves when called with an object value.
Consider these scenarios:
new Promise((resolve) => {
resolve("hello");
});
new Promise((resolve) => {
resolve({a: 100});
});
The key distinction between these two promises lies in the type of value passed to the resolve
function.
Below is the step-by-step procedure for the resolve function based on the ECMAScript specification:
Define F as the current function object.
Ensure that F possesses a [[Promise]] internal slot containing an Object.
Assign promise as F.[[Promise]].
Set alreadyResolved as F.[[AlreadyResolved]].
If alreadyResolved.[[Value]] is true, return undefined.
Update alreadyResolved.[[Value]] to true.
If SameValue(resolution, promise) is true, then
a. Construct a new TypeError object named selfResolutionError.
b. RejectPromise with promise and selfResolutionError.
If Type(resolution) is not Object, then
a. FulfillPromise with promise and resolution.
Retrieve then using Get(resolution, "then").
If then results in an abrupt completion, then
a. RejectPromise with promise and then.[[Value]].
Obtain thenAction from then.[[Value]].
If IsCallable(thenAction) is false, then
a. FulfillPromise with promise and resolution.
Create thenJobCallback using HostMakeJobCallback(thenAction).
Formulate job as NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).
Add job to the queue using HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).
Return undefined.
I am unable to locate the specific treatment within this procedure for cases where an object is provided as an argument without a then
property.