Wait for the completion of asynchronous functions, then execute them sequentially and finally call the last function

Currently, I am utilizing Vue, Node, and TypeScript for my project. One of the challenges I am facing is fetching essential data that multiple functions rely on. The getDataForFunction123() function requires an await operation, which is working fine.

However, I have three separate functions that fetch different data independently, without being dependent on each other. The results from these functions are crucial for the final function, updateAfterFunction123IsDone(). The issue arises when I must wait sequentially for function 1, 2, and 3 to complete before calling updateAfterFunction123IsDone(), resulting in a significant amount of time being wasted.

I am looking for a way to modify this process so that functions 1, 2, and 3 can execute concurrently and notify me when all three are finished, allowing me to then trigger updateAfterFunction123IsDone().

Below is the current code snippet:

async initData () {
  await this.getDataForFunction123();

  await this.function1();
  await this.function2();
  await this.function3();

  this.updateAfterFunction123IsDone();
}

I am skeptical about using Promise.all() to address this problem as it may still execute the functions in order rather than simultaneously. Would this approach only help with error handling rather than optimizing efficiency?

Answer №1

Promise.all triggers once all promises within it are resolved. Execute functions function1, function2, function3 immediately without waiting, and continue once their promises are fulfilled:

async initializeData () {
  await this.fetchDataForFunctions123();

  const promise1 = this.function1();
  const promise2 = this.function2();
  const promise3 = this.function3();

  await Promise.all([promise1, promise2, promise3]);
  this.updateAfterFunctions123Completed();
}

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

Incrementing the index in Javascript when an event occurs

I am currently working on a project that involves incrementing an index of an array based on certain events, such as a left mouse click over a designated area. The code snippet provided below initializes all values to zero and briefly changes the relevan ...

Troubleshooting steps for resolving a node.js error during execution

I recently delved into server side programming with node.js, but I'm encountering some issues when trying to execute it. My server is set up at 127.0.0.1:80, however, I keep running into errors. Console: Server running at http://127.0.0.1:80/ node:ev ...

How does the onclick event trigger even without physically clicking the button?

I am struggling with creating a simple button using mui. My intention is to activate a function only when the button is clicked, but for some reason, as soon as I enter the webpage, it triggers an alert automatically. This behavior is puzzling to me and ...

Creating a JSON-based verification system for a login page

First time seeking help on a programming platform, still a beginner in the field. I'm attempting to create a basic bank login page using a JSON file that stores all usernames and passwords. I have written an if statement to check the JSON file for m ...

Obtain the current route path while statically rendering页面

I'm using Next.js (v14/app router) to create a statically exported website. However, I'm facing an issue with implementing a tab bar in one of my layouts for easy page navigation. Here is a simple example: <div className="tabs"> ...

React is unable to identify the `isDisabled` attribute on a DOM element within an img tag

After updating my React/Next.js App, I encountered the following issue: Upon investigation, React is indicating that the isDisabled prop is not recognized on a DOM element. To resolve this, you can either specify it as lowercase isdisabled if you want it ...

Transmitting extensions via AJAX

I am having trouble sending navigator plugins with AJAX as I am only getting one plugin in the result. The plugin list currently shows: Shockwave Flash. However, it should display like this: Shockwave Flash - Chrome Remote Desktop Viewer - Native Client.. ...

Insert a parameter or substitute the existing value if detected

Trying to create a JavaScript regex that searches for a parameter in a URL and replaces its value if found. If the parameter is not found, it should be added to the URL. Here are a couple of scenarios: http://www.domain.com/?paramZ=123456 https://www.dom ...

What is the best way to transfer a row value from one table to another and then reinsert it back into the original table

I attempted to transfer a row value from one table to another and then back to the original table, but unfortunately, I was unable to find a solution. $('#one tbody tr td input.checkbox').click(function() { if ($(this).attr('checked&apo ...

The occurrence of 'npm run dev' leads to the component getting stuck

After making the decision to transition to a modular Vue.js approach using vue-cli and Webpack, I have been navigating the learning curve of restructuring my application into single file components. Progress has been steady, but I recently encountered a pe ...

Issue with Checkbox Filtering Function [ReactJS]

I'm working on setting up a checkbox filtering feature in React. Here's what I want to achieve: User goes to the products page where all products are displayed initially. User checks a checkbox and only filtered products should be shown. If use ...

What could be causing the data in getServerSideProps to be altered?

After fetching data from an API and passing it to index.js using getServerSideProps, I noticed that the prop array is initially in order by rank [1, 2, 3, etc]. Here's an example of the data: [ {rank: 1, price: 123}, {rank: 2, price: 1958}, {rank: ...

Update the appearance of the stock quantity to show 0 in a vibrant red color

Is there a way to change the appearance of the stock number when it reaches 0, so that it appears in red? Essentially, I have a stock number displayed that increases and decreases as items are added to a cart. However, when the stock number reaches 0, I wa ...

Transfer information(text) to the clipboard using Vue (Nuxt js)

When the vs-button is clicked, I want the value of single_download_link.pdfId to be copied to the clipboard. I attempted it like this, but it did not work. I do not want to use the v-clipboard node module. Can someone please assist me with this? Thank you. ...

Invoking a JavaScript class using a script tag

In my code, I have a class declaration in a script that is imported before the body tag: $(document).ready(function(){ var FamilyTree = function() { }; FamilyTree.prototype.displayMessage=function() { alert("test"); } }); Then, within the bo ...

Replace the icon in Material UI Stepper for steps that have errors

I am utilizing Material UI's Stepper component to display a checklist in this manner. The image below is from their documentation. https://i.sstatic.net/KfUos.png While attempting to add an error state to the checklist, I discovered a prop called er ...

Having trouble with spawning child processes asynchronously in JavaScript

I'm trying to figure out how to format this code so that when a user clicks a button, new input fields and redirect buttons are asynchronously inserted into the unordered list. Everything was working fine until I added the redirect button insertion fu ...

Error message: Unhandled error - $(...).sidr does not exist as a function. [Chrome developer console]

I included this code in the basic module HTML block of a WordPress page builder and encountered the white screen of death. According to the Chrome developer console, the following error occurred: helpers.js?ver=4.5.3:15 Uncaught TypeError: $(...).sidr is ...

jQuery click event handler performing no action

I am encountering an issue with the input on my index page: <input type="button" name="goReplaceAll" value="Go" id="allReplaceGo" style="cursor:hand;" /> There is an onclick call from jQuery as follows: $(document).ready(function() { $(&ap ...

Utilizing AJAX to automatically fill out a form with information retrieved from a MySQL database

I have been attempting to automate the population of a form with data when a gymnast is selected from a dropdown box. I am aware that AJAX needs to be used for this purpose, and despite my best efforts, my lack of proficiency in Javascript has resulted in ...