【问题标题】:Difference how to access the promise差异如何访问承诺
【发布时间】:2016-04-14 07:15:01
【问题描述】:

有没有区别

var source1 = Rx.Observable.of(42);
const oneSubscription = source1.subscribe({
     next: x => console.log(x)
});
oneSubscription.unsubscribe();

和

var source2 = Rx.Observable.of(42);
source2.forEach(x => console.log(x));

我认为要创建一个承诺,你必须先订阅它。

但如果是source2,则无需订阅即可正常工作。

可能有人能解释一下。

【问题讨论】:

    标签: rxjs rxjs5


    【解决方案1】:

    那是因为forEach 也在内部订阅。

    /**
    *  Subscribes an o to the observable sequence.
    *  @param {Mixed} [oOrOnNext] The object that is to receive notifications or an action to invoke for each element in the observable sequence.
    *  @param {Function} [onError] Action to invoke upon exceptional termination of the observable sequence.
    *  @param {Function} [onCompleted] Action to invoke upon graceful termination of the observable sequence.
    *  @returns {Diposable} A disposable handling the subscriptions and unsubscriptions.
    */
    forEach(observer: IObserver<T>): IDisposable;
    

    【讨论】:

    猜你喜欢
    • 2020-12-18
    • 2021-07-11
    相关资源
    最近更新 更多