差异如何访问承诺

Difference how to access the promise

有区别吗
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 的情况下,无需订阅即可运行。

可能有人无法解释。

那是因为 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;