简体   繁体   English

如何等待 observable 完成? 角 2

[英]How to wait for observable to finish? Angular 2

I have multiple calls that are being made and need to wait for all of them to finish before I can route to a different page.我有多个调用,需要等待所有调用完成,然后才能路由到不同的页面。 The problem I am running into is that typically I have been nesting web service calls, but for this case I have separate calls that may or may not be called based on input by the user.我遇到的问题是,通常我一直在嵌套 Web 服务调用,但对于这种情况,我有单独的调用,这些调用可能会或可能不会根据用户的输入进行调用。 How do I wait for all calls that may or may not be called before routing to a new page.在路由到新页面之前,如何等待所有可能会或可能不会被调用的调用。

submitButton_Clicked() {
  this.busy = this.service.call1() // must be called
    .first().subscribe(
      if (success){

        // **Logic to decide if call 2 is needed**
        ...

        this.busy = this.service.call2() // might be called
          .first().subscribe();

        // **Logic to decide if call 3 is needed**
        ...

        this.busy = this.service.call3() // might be called
          .first().subscribe();

        this.router('route'); // should route after all potential calls
      }
    );   
}

I am new to observables, so I'm not sure what the best way of doing this would be.我是 observables 的新手,所以我不确定这样做的最佳方法是什么。 Thanks for the help!谢谢您的帮助!

you could using flatMap你可以使用 flatMap

let Observable = Rx.Observable;

let f = Observable.of(20).delay(1000);
let g = f.map(x => x*x)
  .flatMap(x => Observable.of(x + 100));

g.subscribe(console.log);


/** 
 * Rx.Observable.prototype.flatMap 
 * and Rx.Observable.prototype.selectMany 
 * are equivalent.
 */

let h = f.map(x => x*3)
  .delay(1000)
  .flatMap(x => Observable.of(x + 100));
h.subscribe(console.log);

https://jsbin.com/vokofug/edit?js,console,output https://jsbin.com/vokofug/edit?js,console,output

or concat or merge:或连接或合并:

The concat() stream will print all of the values from source1 first, and only begin printing values from source2 after source1 completes. concat() 流将首先打印 source1 中的所有值,并且仅在 source1 完成后才开始打印 source2 中的值。

The merge() stream will print values from source1 and source2 as it receives them: It won't wait for the first stream to complete before emitting values from the second. merge() 流将在收到来自 source1 和 source2 的值时打印它们:在从第二个流发出值之前,它不会等待第一个流完成。

http://codepen.io/SitePoint/pen/PzKdVQ http://codepen.io/SitePoint/pen/PzKdVQ

'use strict';

const source1 =
  Rx.Observable.interval(100)
    .map(val => `Source 1: ${val}`)
    .take(5);

const source2 =
  Rx.Observable.interval(100)
    .map(val => `Source 2: ${val * 10}`)
    .take(5);

const concat_table = $('#concat-table-body'),
      merge_table  = $('#merge-table-body');

source1
  .concat(source2)
  .subscribe(value => {
    const row = document.createElement('tr');
    row.innerHTML = value;

    // Source 1 values populate BEFORE Source 2 
    concat_table.append(row);
  });

source1
  .merge(source2)
  .subscribe(value => {
    const row = document.createElement('tr');
    row.innerHTML = value;

    // Source 1 values INTERLEAVE with Source 2
    merge_table.append(row);
  });

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

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