简体   繁体   English

等待多个承诺完成

[英]Wait for multiple promises to finish

I am using the SQLStorage from the Ionic platform.我正在使用 Ionic 平台的 SQLStorage。 The remove function returns a promise. remove函数返回一个承诺。 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());

See also https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise/all另见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 .您可以通过提供所有observables/promises的数组来使用rxjsObservable.forkJoin This needs to be done before performing the operation.这需要在执行操作之前完成。 It's similar to Angular 1's $q.all .它类似于 Angular 1 的$q.all

rxjs version <= 6 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 version > 6 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];
});

On rxjs version > 6 You can do something like this:rxjs版本 > 6 上,您可以执行以下操作:

import {forkJoin} from 'rxjs';

and do instead of Observable.forkJoin this:而不是Observable.forkJoin这样做:

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

I'm not familiar with IONIC, but assuming that storage.remove is returning a promise I would suggest you to use forkJoin operator from observables.我不熟悉 IONIC,但假设 storage.remove 正在返回一个承诺,我建议您使用 observables 中的 forkJoin 运算符。

forkJoin takes an array of observables and awaits the execution of all items. forkJoin 接受一组 observables 并等待所有项目的执行。

Just notice that I had to create 3 new observables from each promise returned by the .remove method.请注意,我必须从 .remove 方法返回的每个承诺中创建 3 个新的 observable。

 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]); });

Use Promise.all() :使用Promise.all()

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. Promise.all(iterable) 方法返回一个承诺,该承诺在 iterable 参数中的所有承诺都已解决时解决,或者以第一个通过的承诺拒绝的原因拒绝。

Syntax语法

Promise.all(iterable);

Parameters参数

iterable可迭代的

An iterable object, such as an Array.一个可迭代的对象,例如一个数组。 See iterable.参见可迭代。

use Promise.all for Promisses and Observable.forkJoin for Observables对 Promisses 使用 Promise.all,对 Observables 使用 Observable.forkJoin

as the question is about angular(2+) and you problably should have been using Observable instead of promises.因为问题是关于 angular(2+) 并且您可能应该使用 Observable 而不是 promises。 i`ll add a GET example that worked for me:我将添加一个对我有用的 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)); 

note the use one .map() to handle the response rather than .subscribe()注意使用 .map() 来处理响应而不是 .subscribe()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM