两个单独成功的http请求后激活回调

问题描述 投票:4回答:3

我的应用程序的根组件在init上从我的服务调用两个异步函数来获取数据。我想知道如何在完成后调用函数。我使用角度2.0.0-alpha.44和打字稿1.7.3

import {Component, OnInit} from 'angular2/angular2';

import {ServiceA} from './services/A';
import {ServiceB} from './services/B';


@Component({
  selector: 'app',
  template: `<h1>Hello</h1>`
})
export class App {
  constructor(
    public serviceA: ServiceA,
    public serviceB: ServiceB
  ) { }

  onInit() {

    // How to run a callback, after 
    // both getDataA and getDataB are completed?
    // I am looing for something similar to jQuery $.when()
    this.serviceA.getDataA();
    this.serviceB.getDataB();
  }
}

serviceA.getDataAserviceA.getDataB是简单的http get函数:

// Part of serviceA
getDataA() {
  this.http.get('./some/data.json')
    .map(res => res.json())
    .subscribe(
      data => {
        // save res to variable
        this.data = data;
      },
      error => console.log(error),
      // The callback here will run after only one 
      // function is completed. Not what I am looking for.
      () => console.log('Completed')
    );
}
javascript angular typescript rxjs
3个回答
4
投票

一个简单的并行解决方案是:

let serviceStatus = { aDone: false, bDone: false };

 let getDataA = (callback: () => void) => {
     // do whatver.. 
     callback();
 }

 let getDataB = (callback: () => void) => {
     // do whatver.. 
     callback();
 }

 let bothDone = () => { console.log("A and B are done!");

 let checkServiceStatus = () => {

     if ((serviceStatus.aDone && serviceStatus.bDone) == true)
        bothDone();
 }

 getDataA(() => { 
     serviceStatus.aDone = true;
     checkServiceStatus(); 
});

getDataA(() => { 
     serviceStatus.bDone = true;
     checkServiceStatus(); 
});

我个人使用RxJS让我摆脱这样的棘手情况,可能值得一看。

编辑:

鉴于RxJS实际使用的反馈:

let observable1: Rx.Observable<something>;
let observable2: Rx.Observable<something>;

let combinedObservable = Rx.Observable
    .zip(
        observable1.take(1), 
        observable2.take(1),
        (result1, result2) => {
            // you can return whatever you want here
            return { result1, result2 };
        });

combinedObservable
    .subscribe(combinedResult => {
        // here both observable1 and observable2 will be done.
    });

此示例将并行运行两个observable,并在完成两者时将结果合并为一个结果。


1
投票

您可以在其函数定义中传递getDataA和getDataB回调,然后按顺序调用您想要的任何内容:

function getDataA(callback) {
     // do some stuff
     callback && callback();
}

function getDataB(callback) {
     // do some stuff
     callback && callback();
}

function toCallAfterBoth() {
    // do some stuff
}
getDataA(getDataB(toCallAfterBoth));

0
投票

你可以嵌套函数调用。

例如:

function getDataA (callback) {
    var dataA = {test:"Hello Data A"};
    callback && callback(dataA);
}

function getDataB (callback) {
    var dataB = {test:"Hello Data B"};
    callback && callback(dataB);
}

getDataA(function (dataA) {
    getDataB(function (dataB) {
        console.log("Both functions are complete and you have data:");
        console.log(dataA);
        console.log(dataB);
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.