Could you please direct me to the section in the ECMAScript documentation that specifies the behavior of a resolved promise with its [[Promise

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:

  1. Define F as the current function object.

  2. Ensure that F possesses a [[Promise]] internal slot containing an Object.

  3. Assign promise as F.[[Promise]].

  4. Set alreadyResolved as F.[[AlreadyResolved]].

  5. If alreadyResolved.[[Value]] is true, return undefined.

  6. Update alreadyResolved.[[Value]] to true.

  7. If SameValue(resolution, promise) is true, then

    a. Construct a new TypeError object named selfResolutionError.

    b. RejectPromise with promise and selfResolutionError.

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

    a. FulfillPromise with promise and resolution.

  9. Retrieve then using Get(resolution, "then").

  10. If then results in an abrupt completion, then

    a. RejectPromise with promise and then.[[Value]].

  11. Obtain thenAction from then.[[Value]].

  12. If IsCallable(thenAction) is false, then

    a. FulfillPromise with promise and resolution.

  13. Create thenJobCallback using HostMakeJobCallback(thenAction).

  14. Formulate job as NewPromiseResolveThenableJob(promise, resolution, thenJobCallback).

  15. Add job to the queue using HostEnqueuePromiseJob(job.[[Job]], job.[[Realm]]).

  16. 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.

Answer №1

Step 9 involves retrieving the then property

  1. Assign the value of Get(resolution, "then") to the variable then.

There are four possible outcomes:

  1. If then is not a property of the object, then then (the variable) represents a completion record with [[Value]] as undefined
  2. If evaluating then results in an exception (e.g. it is a getter that throws an exception), then becomes an abrupt completion.
  3. If then exists as a property but is not a function
  4. If then exists as a property and is a function.

Step 10 deals with the case of an abrupt completion:

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

A snippet demonstrating this scenario is provided where then is accessed through a getter resulting in an exception being thrown.

Step 11 assigns the retrieved completion record's [[Value]] field:

  1. Assign the value of then.[[Value]] to thenAction.

Step 12 checks if this value is a function:

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

If the value is not a function, fulfill the main promise with that value.

The subsequent steps handle the scenario where then is indeed a function.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error: 'fs' module not found in React.js and cannot be resolved

Encountering issues with tatum io v1 + react? Developers have acknowledged that it's a react problem which will be addressed in V2. In the meantime, you can utilize tatum io V1 with node js. I've included all dependencies that could potentially ...

Sending data between Node.js middleware components

if (token_count == 1) { var user_name = rows[0].user_name; next(); } else { data = { message: "Invalid Token" } res.send(data); } I must pass the parameter user_name to function next(). When next() is called, it triggers the fo ...

Utilizing variables in GraphQL requests

UPDATE: see the working code below GraphiQL Query I have this query for retrieving a gatsby-image: query getImages($fileName: String) { landscape: file(relativePath: {eq: $fileName}) { childImageSharp { fluid(maxWidth: 1000) { base64 ...

Mastering the Art of Mocking DOM Methods with Jest

What is the best way to simulate this code snippet using Jest : useEffect(() => { document .getElementById('firstname') ?.querySelector('input-field') ?.setAttribute('type', &apos ...

Can Vuejs delay the calculation of a computed property until the component is "ready"?

Within my Vue.js application, I have a `computed` property that relies on a value fetched from an AJAX call. I am looking for a way to delay the calculation of this `computed` property until after the `ready` method has completed. While everything is fun ...

Capturing jQuery ajax requests in Angular

Within my Angular application, I am utilizing the integrated $.couch API from CouchDB. My goal is to intercept every ajax request, regardless of whether it originates from Angular's $http service or jQuery's $.ajax (which is utilized by $.couch). ...

What is the best method for checking if a template has successfully rendered in an Iron:Router route during a mocha test?

I am working on testing if a specific template has rendered in my meteor app for a particular route. My current setup involves iron:router, practicalmeteor:mocha, and I'm using Blaze for rendering. There are a couple of challenges that I am facing: ...

Remove all HTML tags except for those containing a specific class

Looking for a regex that removes all HTML tags except the "a" tags with the "classmark" class For example, given this HTML string: <b>this</b> <a href="#">not match</a> <a href="#" target="_blank" ...

How can I use querySelector in JavaScript to target all div elements that have the same class?

I'm having an issue with the document.querySelector in my JavaScript code. It currently only selects the first div that contains the class "test", but I need it to select all such divs. Is there a way to achieve this using my existing JavaScript? ...

Error: Attempting to access a property of an undefined object using method chaining

I encountered an error of property undefined with the code below. I'm not sure what's causing it. I checked by doing a console.log(navList) in the render and it does have a value. Even after adding if(!navList) return null, I still get the same e ...

Angular's FormGroup for reactive forms is a powerful feature that allows for

Why am I unable to type in the input field before setting a value? html <form action="" [formGroup]="titleForm"> <input class="note-title" type="text" formControlName="title"> </form> ...

Incorporating traditional Bootstrap styling directly into a React application without relying on the react-bootstrap library

My goal is to incorporate my existing bootstrap components into a React project without having to rewrite them to work with the react-bootstrap library. While I have successfully integrated the bootstrap CSS, I am facing issues with the functionality aspec ...

I have a `null` input value. Is there a way to compute this value in relation to a date

When the input is null, what can be done to calculate the value with date? https://i.stack.imgur.com/DmFsJ.png /* // ======================================================== * * * * * How To Calculate Input Value With Date When Input Is Null * * */ // ...

Execute the JavaScript callback

I am encountering an issue when trying to pass an anonymous function as a callback and call it. I seem to be missing something simple, because I keep getting the error 'Uncaught type error - callback is not a function'. Here is what I am attempti ...

Why is it that one function does not hold off on execution until Promise.all() is resolved in another function?

I am currently working on a form that allows users to upload files seamlessly. <form @submit.prevent="sendForm" enctype="multipart/form-data"> <input multiple ref="PostFiles" name="PostFiles" type="file" @change="selectFile('add')"> ...

Javascript: A Fun Game of Questions and Answers

When using JavaScript exclusively, I have an array consisting of four questions, four correct answers, and four incorrect answers. The use of arrays is essential to maintain order in the data. As each question is displayed, a random number is generated by ...

Is there a way to prevent my timer from resetting whenever I refresh the page?

Hey everyone, I'm new to coding and I could really use some help here. I have this code for a timer but I'm struggling to make it work properly even after refreshing the page. My goal is to keep the timer running smoothly, but I'm not sure w ...

Map Row is unreturned

I am having trouble when attempting to map a JSON response from a MySQL query as I am receiving no response: data: NULL This is the code in question: const audience = rows.map((row) => { db.query(CountAudiences, [row.campaign], function(err, count ...

Exploring the world of Next.js version 9.3 and beyond with the exciting addition

As a beginner with Next.js, I am seeking guidance on utilizing getStaticPaths and getStaticProps within catch-all routes. Many blog starters for Next.js 9.3+ focus on single-level blog posts (such as /posts/post-1.md, /posts/post-2.md, etc.), but I am stru ...

Modifying Image on Tab Click using jQuery

In my current WordPress project, I am working on dynamically changing an image based on the tab that is clicked. I would like to use jQuery's fade effect to smoothly replace the image with a new one that is relative to the specific tab being clicked. ...