Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To resolve multiple promise in typescript

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

like image 909
Pavan Tiwari Avatar asked Jan 18 '17 05:01

Pavan Tiwari


People also ask

Can you resolve a Promise multiple times?

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.

How do you run multiple promises at once?

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.

Can you await a Promise twice?

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.


1 Answers

Promise.all([
    //task1,
    //task2,
    //task3,
  ]).then((value) => {doSomething()});

task can be any Promise call

like image 152
ani5ha Avatar answered Oct 06 '22 00:10

ani5ha