0

有没有区别

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事情只是在没有订阅的情况下工作。

可能有人可以解释。

4

1 回答 1

2

那是因为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;
于 2016-04-14T08:16:56.237 回答