Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make my parallel call in Javascript

I am building a shop service that updates the status of products that was bought by customers. I want to update the calls at once .i.e run them in parallel but i do not know the best way. Pls any one to help. I am using Typescript and i do not want to use a loop

tried this
for ( let i=0;i<10; i++ ) { // make calls }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can run the calls in parallel using promise all

Look at this example

    const allPromise = Promise.all([promise1, promise2]);
    
    allPromise.then(values => {
      console.log(values); // [resolvedValue1, resolvedValue2]
    }).catch(error => {
      console.log(error); // rejectReason of any first rejected promise
    })

Input: Promise.all() takes an array of promises as its argument. Each promise represents an asynchronous operation that may or may not resolve successfully.

Concurrent Execution: Once Promise.all() is called, all the promises in the input array are executed concurrently, meaning they start executing at the same time without waiting for each other to complete.

Resolving or Rejecting: While the promises are executing, they can either resolve (fulfill) with a value or reject with an error. The execution of each promise is independent of the others.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading