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