Do promises within an array begin execution only when they are passed to Promise.all()?

When an array of promises is built and then passed to the Promise.all method, does execution start immediately or only once Promise.all is called?

Consider the following code snippet:

let promises: Promise<string> [] = [this.methodCall(), this.methodCall(), this.methodCall()];

 Promise.all(promises).then(values => {
  ...
 }).catch(error => {
  ...
 }); 

In addition, can this code example catch all rejections that may occur?

Answer №1

Upon creation, a Promise gets to work right away. Take a look at the logs. The body function of the Promise is executed first, and then after 5 seconds, the Promise.all begins.

const pr = new Promise((resolve, reject) => {
  console.log('Started !!!');
  resolve();
});

console.log('Before setTimeout');

setTimeout( () => {
  Promise.all([pr]).then(result => console.log('Resolved !!!'));
}, 5000)

console.log('After setTimeout');

Referenced from the Documentation

A function that is passed with the arguments resolve and reject. The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object).

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

An issue has been encountered in NodeJS with a route that begins with a percent sign causing an error

I have a NodeJS server set up with the following backend configuration: app.use(express.static(__dirname)) app.get('/', function(req, res){ res.send('main page here') }); app.get('/*', function(req, res){ res.send(&apos ...

What steps do I need to take to ensure that my angular application can be displayed properly on Internet Explorer

I am facing an issue with my application not rendering in ie8. I have added all the necessary shim and shiv scripts, but it still doesn't work. The strange thing is that my application runs perfectly on every other browser version above ie9. Any help ...

Using onChange input causes the component to re-render multiple times

As I delve into learning React, I've encountered some challenges. Despite thinking that I grasped the concept of controlled components, it appears that there's a misunderstanding on my end. The issue arises after an onChange event where I notice ...

How to access localStorage within an AngularJS module

Upon successful login, I save the user's data in the localStorage and redirect them to the Dashboard. LoginCtrl: (function() { 'use strict'; angular.module('BlurAdmin.pages.login') .controller('LoginCtrl& ...

When using Asp.Net TextBox, the focus is lost after inputting the first character

I have created an Asp.Net web form (aspx) that includes a TextBox: <div id="field"> <asp:TextBox Id="Name" runat="server" MaxLength="50"/> </div> Whenever I type characters into the TextBox, I t ...

receiving an object as the return value in AngularJS

To access locfrnd in the code snippet below, follow these steps: I have created an Array named PlaceCollection containing 2 elements. place locfrnd, which is an array While I was able to successfully access place, I encountered an error when trying to a ...

Continuously cycling without progressing to the subsequent directory

My goal is to create a library function that utilizes a JSON file generated by the directory tree tool to recursively iterate through each folder in order to construct an interactive <ul> list. During testing, I noticed that it gets stuck in an infin ...

The preventDefault() function is not functioning properly on the <a> tag

As a JavaScript beginner, I decided to create an accordion menu using JavaScript. Although I was successful in implementing it, I encountered a bug in my program. In this scenario, uppercase letters represent first-level menus while lowercase letters repr ...

"React - encountering issues with state being undefined when passing child state up through parent components

I am currently navigating the world of react and have encountered a hurdle. I find myself facing difficulties in updating the parent component based on changes in the child state. I was able to pass the child state to the parent by linking the child's ...

Functionality of local data storage

I'm currently exploring the capabilities of the localStorage feature. Let's say I have a large JSON object stored using localStorage. I need to share this data with the testing team for review. However, if I try to display the stored data on an H ...

Vue project encounters disappeared DOM element

I'm currently developing a code typer, but I've encountered an issue where the element inexplicably turns into Null. Despite having some experience with Vue from my previous project on Django/Python at (live preview), I find myself struggling to ...

Creating Empathetic User Experiences with Next 12 and SWC: A Guide to Harnessing import.meta.url

In my Next.js 12 App with the Rust Compiler, I am utilizing Jest and WebWorkers. In one of my files, I am using import.meta.url. to create the worker. The issue arises when Jest throws an error, stating that import.meta.url cannot be used outside of an ES ...

Displaying the specified item within an array using AngularJS

My application features a group of 'players' each with their own unique attributes that I want to showcase within a modal window and cycle through. The user interface is structured like this: <!-- Score Round Modal --> <div class="mo ...

Discover the power of integrating JSON and HTTP Request in the Play Framework

My aim is to develop a methodology that can effectively manage JSON and HTTP requests. This approach will facilitate the transition to creating both a Webapp and a Mobile App in the future, as JSON handling is crucial for processing requests across differe ...

The constant reloading of localhost is due to the connection of Mongodb with Node.js

I am facing an issue while trying to establish a connection between my Node.js Express script and the backend MongoDB 5.0.5. When I run the app.js using nodemon, it shows that the MongoDB is connected; however, on the localhost, the page keeps reloading wi ...

Search for the text using jQuery and then conceal it

I am attempting to conceal specific text within paragraphs, however, the issue is that the script identifies the entire paragraph and removes it completely. My goal is to only remove the text that has been identified. Check out the demo here <div cla ...

Could someone assist me with understanding how to use the Load function in JQuery?

I have a set of links with the class "ajax" that are meant to fetch content from an element with the id "test" in the file "data.html" located in the same directory. This content should then be inserted into the div with the id "content" on my page: <s ...

Tips for including MIME type in headers

Everything was running smoothly with my C# web application. However, to enhance security measures, I decided to add the "nosniff" custom header in the web.config file. <system.webServer> </handlers> <httpProtocol> &l ...

Broken Mui Input - Full width with attributes for minimum and maximum values

I've created a sandbox to demonstrate an issue I came across that seems unbelievable, but it's happening. Here's the link: https://codesandbox.io/s/nifty-swanson-yxj4n2?file=/NumberField.js:1075-1097 The problem is simple - when both the ht ...

Angular JavaScript does not take effect on elements inserted into ui-view

When transferring the code from main.html to index.html, the javascript functions smoothly. The click function successfully triggers the ripple effect. Link: Ripple Click Effect Google Material Design However, upon extracting the code to main.html and in ...