简体   繁体   English

可观察的订阅未调用

[英]Observable subscription not getting called

I have an Observable that I'm using to convert a promise into a subscription. 我有一个Observable,用于将诺言转换为订阅。 This results in a collection that I need to iterate through to call an HTTP Service on each element. 这导致我需要迭代一个集合以在每个元素上调用HTTP服务。 I'm using forkJoin to wait for all those calls to finish so that I can do something else, but unfortunately, my subscription is not being called. 我正在使用forkJoin等待所有这些调用完成,以便我可以做其他事情,但是不幸的是,我的订阅没有被调用。 Do you see what I'm missing here? 你看到我在这里想念的吗?

Observable.fromPromise(this.users.getElements()).subscribe(results => {
  Observable.forkJoin(
    results.map(
      aUser => this.HttpService.submitUser(aUser).subscribe(
        results => {
          this.progress += 1;
        },
        err => {
          this.progress += 1;
          this.handleError(<any>err);
        })
    ).subscribe(
      //it never gets to either of these calls after all service calls complete
      data => {
        debugger;
        console.log(data);
        this.reset();
      },
      err => {
        debugger;
        console.log(err);
        this.reset();
      }
    ));
});

One thing is you don't subscribe to each Observable passed to forkJoin() . 一件事是您不订阅传递给forkJoin()每个Observable。 The operator has to do it itself. 操作员必须自己做。

If you want to be notified when each Observable completes you can use .do(undefined, undefined, () => {...}) . 如果要在每个Observable完成时收到通知,则可以使用.do(undefined, undefined, () => {...})

let observables = [
  Observable.of(42).do(undefined, undefined, () => console.log('done')),
  Observable.of('a').delay(100).do(undefined, undefined, () => console.log('done')),
  Observable.of(true).do(undefined, undefined, () => console.log('done')),
];

Observable.forkJoin(observables)
  .subscribe(results => console.log(results));

This prints to console: 打印到控制台:

done
done
done
[ 42, 'a', true ]

Eventually there's also .finally() operator. 最终还有.finally()运算符。 However, it's not the same as using .do() . 但是,它与使用.do()

EDIT: 编辑:

When any of the source Observables fail the forkJoin() operator reemits the error (which means it also fails). 当任何源Observable失败时, forkJoin()运算符将重新forkJoin()错误(这意味着它也将失败)。
This means you need to catch errors in each source Observable separately (with catch() operator for example). 这意味着您需要分别捕获每个Observable源中的错误catch()例如,使用catch()运算符)。

let observables = [
  Observable.throw(new Error())
    .catch(() => Observable.of('caught error 1'))
    .do(undefined, undefined, () => console.log('done 1')),

  Observable.of('a')
    .delay(100).catch(() => Observable.of('caught error 2'))
    .do(undefined, undefined, () => console.log('done 2')),

  Observable.of(true)
    .catch(() => Observable.of('caught error 3'))
    .do(undefined, undefined, () => console.log('done 3')),
];

Observable.forkJoin(observables)
  .subscribe(results => console.log(results));

Which prints: 哪些打印:

done 1
done 3
done 2
[ 'caught error 1', 'a', true ]

I don't think you need to subscribe in the map. 我认为您不需要订阅地图。

Observable.fromPromise(this.users.getElements()).subscribe(results => {
  Observable.forkJoin(
    results.map(
      aUser => this.HttpService.submitUser(aUser))
    ).subscribe(
      //it never gets to either of these calls after all service calls complete
      data => {
        debugger;
        console.log(data);
        this.reset();
      },
      err => {
        debugger;
        console.log(err);
        this.reset();
      }
    ));
});

Note that in the rxjs example here: 请注意,在此处的rxjs示例中:

https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/forkjoin.md

they don't subscribe to the individual observables -- ForkJoin gets them all going, then waits for all of them to return (in your subscribe.) 他们不订阅各个可观察对象-ForkJoin使它们全部进行,然后等待它们全部返回(在您的订阅中)。

EDIT: 编辑:

The forkjoin source is here: forkjoin源在这里:

https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/linq/observable/forkjoin.js https://github.com/Reactive-Extensions/RxJS/blob/master/src/core/linq/observable/forkjoin.js

and it doesn't look like it has hooks to find out when each finishes. 而且看起来好像没有钩子可以确定何时完成。 I think the best way to approach a UI bar would be to have each of the mapped observables subscribed to individually that all call a function to increment the UI counting bar variable and have some test for "completeness" that allows you to use the data. 我认为,处理UI栏的最佳方法是分别订阅每个映射的可观察对象,它们都调用一个函数来增加UI计数栏变量并进行一些“完整性”测试,以允许您使用数据。

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

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