我正在使用来自 Ionic 平台的 SQLStorage。 remove
函数返回一个 promise 。在我的代码中,我需要删除多个值。当这些都完成后,我需要执行一些代码。
我怎样才能等待所有这些然后执行回调函数?
代码:
removeAll() {
this.storage.remove(key1);
this.storage.remove(key2);
this.storage.remove(key3);
}
全部嵌套是一种不好的做法,所以我正在寻找一个合适的解决方案:)
removeAll() {
return this.storage.remove(key1).then(() => {
this.storage.remove(key2).then(() => {
this.storage.remove(key3);
});
});
};
最佳答案
你可以使用
removeAll() {
Promise.all([
this.storage.remove(key1),
this.storage.remove(key2),
this.storage.remove(key3),
]).then(value => doSomething());
另见 https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all
关于javascript - 等待多个 promise 完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37841721/