等待多个承诺完成
Wait for multiple promises to finish
我正在使用来自 Ionic 平台的 SQLStorage。 remove
函数 returns 一个承诺。在我的代码中,我需要删除多个值。当这些都完成后,我需要执行一些代码。
如何等待所有这些然后执行回调函数?
代码:
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);
});
});
};
您可以通过提供所有 observables/promises
的数组来使用 rxjs
中的 Observable.forkJoin
。这需要在执行操作之前完成。它类似于 Angular 1 的 $q.all
.
rxjs 版本 <= 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 版本 > 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];
});
The Promise.all(iterable) method returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects.
Syntax
Promise.all(iterable);
Parameters
iterable
An iterable object, such as an Array. See iterable.
您可以使用
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
我不熟悉 IONIC,但假设 storage.remove 正在返回一个 promise,我建议您使用来自 observables 的 forkJoin 运算符。
forkJoin 接受一组可观察对象并等待所有项目的执行。
请注意,我必须根据 .remove 方法返回的每个承诺创建 3 个新的可观察对象。
Observable.forkJoin([
Observable.fromPromise(this.storage.remove(key1)),
Observable.fromPromise(this.storage.remove(key2)),
Observable.fromPromise(this.storage.remove(key3))
])
.subscribe(data => {
console.log(data[0]);
console.log(data[1]);
console.log(data[2]);
});
对 Promise 使用 Promise.all,对 Observable 使用 Observable.forkJoin
因为问题是关于 angular(2+) 的,您可能应该一直使用 Observable 而不是 promises。我将添加一个对我有用的 GET 示例:
import {Observable} from 'rxjs/Rx';
Observable.forkJoin(
this._dataService.getOne().map(one => this.one =one),
this._dataService.getTwo().map(two => this.two two),
this._dataService.getN().map(n => this.n = n),
)
).subscribe(res => this.doSomethingElse(this.one, this.two, this.n));
注意使用一个 .map() 来处理响应而不是 .subscribe()
在 rxjs
版本 > 6 你可以这样做:
import {forkJoin} from 'rxjs';
并代替 Observable.forkJoin
这样做:
forkJoin([
this.service1.get(),
this.service2.get()
]).subscribe(data => {
this.data1= data[0];
this.data2 = data[1];
我正在使用来自 Ionic 平台的 SQLStorage。 remove
函数 returns 一个承诺。在我的代码中,我需要删除多个值。当这些都完成后,我需要执行一些代码。
如何等待所有这些然后执行回调函数?
代码:
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);
});
});
};
您可以通过提供所有 observables/promises
的数组来使用 rxjs
中的 Observable.forkJoin
。这需要在执行操作之前完成。它类似于 Angular 1 的 $q.all
.
rxjs 版本 <= 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 版本 > 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];
});
The Promise.all(iterable) method returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects.
Syntax
Promise.all(iterable);
Parameters
iterable
An iterable object, such as an Array. See iterable.
您可以使用
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
我不熟悉 IONIC,但假设 storage.remove 正在返回一个 promise,我建议您使用来自 observables 的 forkJoin 运算符。
forkJoin 接受一组可观察对象并等待所有项目的执行。
请注意,我必须根据 .remove 方法返回的每个承诺创建 3 个新的可观察对象。
Observable.forkJoin([
Observable.fromPromise(this.storage.remove(key1)),
Observable.fromPromise(this.storage.remove(key2)),
Observable.fromPromise(this.storage.remove(key3))
])
.subscribe(data => {
console.log(data[0]);
console.log(data[1]);
console.log(data[2]);
});
对 Promise 使用 Promise.all,对 Observable 使用 Observable.forkJoin
因为问题是关于 angular(2+) 的,您可能应该一直使用 Observable 而不是 promises。我将添加一个对我有用的 GET 示例:
import {Observable} from 'rxjs/Rx';
Observable.forkJoin(
this._dataService.getOne().map(one => this.one =one),
this._dataService.getTwo().map(two => this.two two),
this._dataService.getN().map(n => this.n = n),
)
).subscribe(res => this.doSomethingElse(this.one, this.two, this.n));
注意使用一个 .map() 来处理响应而不是 .subscribe()
在 rxjs
版本 > 6 你可以这样做:
import {forkJoin} from 'rxjs';
并代替 Observable.forkJoin
这样做:
forkJoin([
this.service1.get(),
this.service2.get()
]).subscribe(data => {
this.data1= data[0];
this.data2 = data[1];