Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
Where does say in ECMAScript that resolved promise has [[PromiseResult]] equal to object?

I am trying to match the workings of a promise's resolve function against the corresponding procedure in the ECMAScript specification. More specifically, I am looking for the specification in the case the resolve function is called with an object value.

Take these examples:

new Promise((resolve) => {
    resolve("hello");
});
new Promise((resolve) => {
    resolve({a: 100});
});

There is one difference between these two promises: the type of value passed to the resolve function.

Here is the procedure for the resolve function from the ECMAScript specification:

  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. Return RejectPromise(promise, selfResolutionError).

  8. If Type(resolution) is not Object, then

    a. Return FulfillPromise(promise, resolution).

  9. Let then be Get(resolution, "then").

  10. If then is an abrupt completion, then

    a. Return RejectPromise(promise, then.[[Value]]).

  11. Let thenAction be then.[[Value]].

  12. If IsCallable(thenAction) is false, then

    a. Return FulfillPromise(promise, resolution).

  13. Let thenJobCallback be HostMakeJobCallback(thenAction).

  14. Let job be NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).

  15. Perform HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).

  16. Return undefined.

I cannot pinpoint where the case is treated that has an object as argument without a then property.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Step 9 gets the then property

  1. Let then be Get(resolution, "then").

There are mainly four outcomes possible:

  1. then is not a property of the object, so then (the variable) represents a completion record with [[Value]] undefined
  2. then is a property of the object, but evaluating it produces an exception (e.g. it is a getter, and the getter runs into an exception). then is then a so-called abrupt completion.
  3. then is a property of the object, but it is not a function
  4. then is a property of the object, and it is a function.

Step 10 checks for one of these outcomes:

  1. If then is an abrupt completion, then
          a. Return RejectPromise(promise, then.[[Value]]).

Here is a snippet that makes 10.a happen:

let obj = {
    get then() {
        throw "sorry";
    }
};

new Promise(resolve => resolve(obj))
   .catch((err) => console.log("rejected with ", err))

This represents the case where then is not necessarily intended to evaluate as a function (it is a getter, so code must execute to determine that), but accessing it makes it impossible to know whether it would be a function or not, since it produces an exception. The specification mentions the execution of the getter in [[Get]]

Step 11 is a simple assignment of the [[Value]] field of the retrieved completion record (note that there are also other fields, like [[Type]])

  1. Let thenAction be then.[[Value]].

Step 12 checks whether this value is a function:

  1. If IsCallable(thenAction) is false, then
          a. Return FulfillPromise(promise, resolution).

If not a function, the main promise is fulfilled with that object as value.

The remaining steps deal with the case where then is a function.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!