Wait for multiple promises to finish Wait for multiple promises to finish angular angular

Wait for multiple promises to finish


You can use

removeAll() {  Promise.all([    this.storage.remove(key1),    this.storage.remove(key2),    this.storage.remove(key3),  ]).then(value => doSomething());

See also https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all


You could use Observable.forkJoin from rxjs by providing an array of all the observables/promises. This needs to be done before performing the operation. It's similar to Angular 1's $q.all.

rxjs version <= 6

Observable.forkJoin([   this.storage.remove(key1),    this.storage.remove(key2),   this.storage.remove(key3)]).subscribe(t=> {    var firstResult = t[0];    var secondResult = t[1];});

rxjs version > 6

import {forkJoin} from 'rxjs';forkJoin([   this.storage.remove(key1),    this.storage.remove(key2),   this.storage.remove(key3)]).subscribe(t=> {    var firstResult = t[0];    var secondResult = t[1];});


On rxjs version > 6 You can do something like this:

import {forkJoin} from 'rxjs';

and do instead of Observable.forkJoin this:

forkJoin([  this.service1.get(),  this.service2.get()]).subscribe(data => {  this.data1= data[0];  this.data2 = data[1];