Wait for multiple promises to finish
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];
});
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