I have a service, which first loads a config file from server through an $http call then in it's success call back I am reading the file content, which actually contains a list of files to load from the server.
So I make another call to server to load all other files. Is there any way in TypeScript to return a promise which will get called only when all the promises are get resolved. Same like $q.all we had in angular 1.X
No. It is not safe to resolve/reject promise multiple times. It is basically a bug, that is hard to catch, becasue it can be not always reproducible.
In this approach, we will use Promise. all() method which takes all promises in a single array as its input. As a result, this method executes all the promises in itself and returns a new single promise in which the values of all the other promises are combined together.
The implication of this is that promises can be used to memoize async computations. If you consume a promise whose result will be needed again later: consider holding on to the promise instead of its result! It's fine to await a promise twice, if you're happy to yield twice.
Promise.all([
//task1,
//task2,
//task3,
]).then((value) => {doSomething()});
task can be any Promise call
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With