3 回答

TA貢獻1906條經驗 獲得超10個贊
RxJS 中的一切都是同步的,除非你處理延遲或者你有意使用異步調度程序,例如asyncScheduler
.
當你有一個 RxJS 鏈時,它是同步的還是異步的只取決于你使用的操作符和你的源 Observables 的行為。所以這不是of()
or的任何特定內容from()
。即使from([1, 2, 3])
orof(1, 2, 3)
發出三個值,它也會同步發出。
另請注意,您無法將異步鏈變為同步行為。

TA貢獻1946條經驗 獲得超4個贊
這里有一個輕微的誤解(正如 Kaustubh 所暗示的):of
不會立即返回其所有值。只是of
可以接受可變數量的參數,而只from
需要一個。
of
將每個給定的參數解釋為值,同時from
會將單個給定的參數轉換為可觀察的。
所以這表現相同:
of(1, 2) from([1, 2])
雖然這不會:
of([1, 2]) from([1, 2])

TA貢獻1871條經驗 獲得超8個贊
考慮以下,
const myValues = ['val1', 'val2', 'val3'];
const atOnce = Observable.of(myValues); // This will emit the entire myValues array as a single emission.
// The Observables below are similar in behaviour
// The will emit the values from myValues one at a time
const discreteOne = Observable.of(...myValues);
const discreteTwo = Observable.from(myValues);
添加回答
舉報