Essentially, the observable creation functions of
, from
, and others simply create a new Observable()
with specific behavior.
So, the only difference between new Observable()
and the creator fuctions is the actual behavior.
For example, here is what the of()
function looks like (simplified):
export function of<T>(...array: Array<T>): Observable<T> {
return new Observable<T>(subscriber => {
for (let i = 0; i < array.length && !subscriber.closed; i++) {
subscriber.next(array[i]);
}
subscriber.complete();
});
}
You can see that new Observable()
is called within the of()
function.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…