AustinTX
AustinTX

Reputation: 1352

How to execute functions parallel in typescript-es5

I have a list of functions in angular2/typescript app called as: f1, f2, f3, f4 etc... These functions can be executed in any order and all of them return void.

I am thinking run them in parallel but don't know how to do that with target ES5.

What is the best way to execute these functions?

thanks,

Austin

Upvotes: 3

Views: 8282

Answers (2)

Mark van Straten
Mark van Straten

Reputation: 9425

Since all your functions are most likely promises and you don't care about the output or order, only that all are executed i would suggest using Promise.all():

Promise.all([f1(), f2(), f3(), f4(), f5()])
  .then(_ => console.log('all completed'));

This will let the promises run in background and only when all complete report back to you.

Upvotes: 2

Günter Zöchbauer
Günter Zöchbauer

Reputation: 658263

In the browser the is only one UI thread. If you run code in the UI thread, there won't ever run anything in parallel.

If you want to run code in parallel, you can utilize web workers. A single web worker is also just one thread, if you want to run multiple functions in parallel, you need a new web worker instance for each.

I don't know the current state of web worker support.

For more details see for example

Upvotes: 3

Related Questions