I am using the SQLStorage from the Ionic platform. The remove
function returns a promise. In my code I need to remove multiple values. When these are all finished I need to execute some code.
How can I wait for all of these and then execute a callback function?
Code:
removeAll() {
this.storage.remove(key1);
this.storage.remove(key2);
this.storage.remove(key3);
}
Nesting all is a bad practise so I am looking for a decent solution :)
removeAll() {
return this.storage.remove(key1).then(() => {
this.storage.remove(key2).then(() => {
this.storage.remove(key3);
});
});
};
You can use
removeAll() {
Promise.all([
this.storage.remove(key1),
this.storage.remove(key2),
this.storage.remove(key3),
]).then(value => doSomething());
value
is an array that contains the return values of each async call passed to Promise.all([...])
matching in order.
See also https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all