Angular2等待多个承诺完成

时间:2021-09-28 03:26:18

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.

我正在使用Ionic平台的SQLStorage。 remove函数返回一个promise。在我的代码中,我需要删除多个值。当这些都完成后,我需要执行一些代码。

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

    });

  });

};

6 个解决方案

#1


49  

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

#2


22  

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.

您可以通过提供所有observables / promises的数组,从rxjs中使用Observable.forkJoin。这需要在执行操作之前完成。它类似于Angular 1的$ q.all。

Observable.forkJoin([
   this.storage.remove(key1), 
   this.storage.remove(key2),
   this.storage.remove(key3)
])
.subscribe(t=> {
    var firstResult = t[0];
    var secondResult = t[1];
});

#3


3  

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返回一个promise,我建议你使用来自observables的forkJoin操作符。

ForJoin takes an array of observables and awaits the execution of all items.

ForJoin获取一系列可观察数据并等待所有项目的执行。

Just notice that I had to create 3 new observables from each promise return by the .remove method.

请注意,我必须通过.remove方法从每个promise返回创建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]);
});

#4


2  

use Promise.all for Promisses and Observable.forkJoin for Observables

使用Promise.all for Promisses和Observable.forkJoin for Observables

as the question is about angular(2+) and you problably should have been using Observable instead of promises. i`ll add a GET example that worked for me:

因为问题是关于角度(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)); 

note the use one .map() to handle the response rather than .subscribe()

注意使用一个.map()来处理响应而不是.subscribe()

#5


1  

Use 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)方法返回一个promise,它在iterable参数中的所有promise都已解析时解析,或者拒绝第一个传递的拒绝promise的原因。

Syntax

Promise.all(iterable);

Parameters

iterable

An iterable object, such as an Array. See iterable.

可迭代对象,例如Array。见iterable。

#6


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

#1


49  

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

#2


22  

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.

您可以通过提供所有observables / promises的数组,从rxjs中使用Observable.forkJoin。这需要在执行操作之前完成。它类似于Angular 1的$ q.all。

Observable.forkJoin([
   this.storage.remove(key1), 
   this.storage.remove(key2),
   this.storage.remove(key3)
])
.subscribe(t=> {
    var firstResult = t[0];
    var secondResult = t[1];
});

#3


3  

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返回一个promise,我建议你使用来自observables的forkJoin操作符。

ForJoin takes an array of observables and awaits the execution of all items.

ForJoin获取一系列可观察数据并等待所有项目的执行。

Just notice that I had to create 3 new observables from each promise return by the .remove method.

请注意,我必须通过.remove方法从每个promise返回创建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]);
});

#4


2  

use Promise.all for Promisses and Observable.forkJoin for Observables

使用Promise.all for Promisses和Observable.forkJoin for Observables

as the question is about angular(2+) and you problably should have been using Observable instead of promises. i`ll add a GET example that worked for me:

因为问题是关于角度(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)); 

note the use one .map() to handle the response rather than .subscribe()

注意使用一个.map()来处理响应而不是.subscribe()

#5


1  

Use 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)方法返回一个promise,它在iterable参数中的所有promise都已解析时解析,或者拒绝第一个传递的拒绝promise的原因。

Syntax

Promise.all(iterable);

Parameters

iterable

An iterable object, such as an Array. See iterable.

可迭代对象,例如Array。见iterable。

#6


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