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
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
Share Improve this question edited Jan 20, 2017 at 20:44 Mark van Straten 9,4253 gold badges40 silver badges57 bronze badges asked Jan 20, 2017 at 3:47 AustinTXAustinTX 1,3525 gold badges23 silver badges28 bronze badges 3-
if they are asynchronous ...
f1(); f2(); f3(); f4();
will start them all without waiting for the preceding one to finish – Jaromanda X Commented Jan 20, 2017 at 3:51 - How to create async function if we target typescript to support ES5? – AustinTX Commented Jan 20, 2017 at 4:09
-
How to create async function
that's irrelevant if f1...f4 already do some asynchronous something – Jaromanda X Commented Jan 20, 2017 at 4:15
2 Answers
Reset to default 3In 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
- Run Angular 2 app in web worker using WebPack
- http://www.syntaxsuccess./viewarticle/web-workers-in-angular-2.0
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 pleted'));
This will let the promises run in background and only when all plete report back to you.