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

javascript Arabic speech synthesis

Attempting to utilize SpeechSynthesisUtterance for Arabic language Successfully functioning with English content $(document).ready(function() { var u1 = new SpeechSynthesisUtterance('Hello world!'); u1.lang = 'en-US'; u1.pitch ...

Refreshing Angular Page

I'm looking for a way to reset my Angular page back to its original state with just one button click. I attempted to use the angular.copy method, but encountered an error. I have various scope and controller variables that I don't want to reset i ...

NextJS was throwing a warning at me, while Firebase hosting was giving me an error regarding the absence of unique keys for children in lists, even though I

I've been troubleshooting this warning for quite some time, but I can't seem to resolve it. The warning reads as follows: Warning: Each child in a list should have a unique "key" prop. Check the top-level render call using <ul>. ...

Guide to showing a preview of an image prior to submitting a form using vue.js

I created a Vue form but I'm having trouble making the image preview function work when users try to submit an image to the form. After refreshing the page, I can see the image, but when uploading it, the preview link is broken. There is an option to ...

commenting system through ajax across multiple web pages

Experimenting with the webcodo comment system has led me to this URL: Database table: "comments" CREATE TABLE IF NOT EXISTS `comments` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(40) NOT NULL, `email` varchar(60) NOT NULL, `comment` te ...

Menu options are unresponsive to clicks in the dropdown

Need help fixing my dropdown menu issue. Whenever I hover over the dropdown, it disappears before I can click on any items. Here is a snippet of the code causing the problem: #navContainer { margin: 0; padding: 0; padding-top: 17px; width: 220 ...

Can a YouTube video be triggered to play when a specific CSS style is selected?

I am searching for a method to display and play different YouTube videos based on a selected CSS style from a drop-down menu. I believe JavaScript can be utilized to detect the chosen CSS. Include the following in the html header of the page. <script ...

When the user initiates a fetch request, they will be directed to the POST page as a standard

Whenever I utilize fetch for a post request, I encounter an issue where the user is not redirected to the Post page as they would be with a regular form submission. My goal is to be able to direct the user to a specific location on the server side at app ...

Implementing dynamic image insertion on click using a knockout observable

I'm utilizing an API to retrieve images, and I need it to initially load 10 images. When a button is clicked, it should add another 10 images. This is how I set it up: Defining the observable for the image amount: public imageAmount: KnockoutObserva ...

What is the best way to initialize React-map-gl with the user's current location as the default latitude and longitude?

Is there a way to render the map with default values of viewport set to user's location without needing to click a locate me button? <ReactMapGL mapboxApiAccessToken={mapboxApiKey} mapStyle="mapbox://styles/mapbox/streets-v11" ...

Execute JavaScript using "matches" without the need for the page to refresh

I have been developing a school project which involves creating a Chrome extension to streamline the Checkout process on a website. In my manifest.json file, I specified that a content.js file should run when it matches a specific URL. "content_script ...

Ways to inform a subelement that a task is complete?

I have a child component that displays a list of orders passed from the parent. I want to include a "Reload" button inside the child component that, when clicked, triggers a function in the parent component to reload the orders. Upon clicking the reload b ...

Jquery UI sortable can determine the vertical orientation, either moving items up or down

I am having difficulty determining the direction in which axis N is moving, whether up or down. I would also like the elements (h2 and p) inside to remain in their positions. Can anyone provide assistance? http://jsfiddle.net/zerolfc/y62awe4u/2/ <div ...

Having trouble decoding JWE using the NPM Jose library

Although I can successfully decrypt a JWE using an older version of jose, I'm facing difficulties in utilizing the latest version API. The headers of my token are as follows: { "alg": "A128KW", "enc": "A128CBC-H ...

What is the established procedure for resetting all elements within an (X)HTML document?

Is there a way to reset elements without using a form like how it can be done with JavaScript? document.forms[0].reset(); I am utilizing AJAX, so do I need to loop through all the elements using JavaScript? ...

What steps should I take to ensure that pull is functioning correctly with mongoose and mongodb?

I am facing an issue while trying to retrieve an object from an array within a Model. Despite verifying my query params and confirming that they are correct, I am unable to get it to function as expected. Any assistance on this matter would be highly appre ...

AngularJS bracket-enhanced template

Why is AngularJS giving an error when brackets are used inside ng-template content? I am trying to create an input field that should accept an array, but I keep getting this error message: "Error: Syntax Error: Token ']' not a primary expression ...

What is the best way to execute multiple Protractor test suites simultaneously?

Exploring Protractor for the first time. My goal is to run multiple test suites in a sequence. I have a complex angular form with various scenarios, each with its expected results. I want to execute all tests with just one command. Initially, I tried enter ...

Harnessing the Power of JSON Data Extraction with JavaScript

I stored the data in JSON format using the setItem method: localStorage.setItem('orderproduct', JSON.stringify([{imageSource: productImg, productTitle: title, productQuantity: qty, productPrice: finalprice}])); When I inspect it, this is how it ...

When the mouse hovers over the slider, the final image jumps into view

After successfully building an image slider that displays 2 partial images and 1 full image on hover, I encountered a bug when setting the last image to its full width by default. This caused a temporary gap in the slider as the other images were hovered o ...