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);
添加回答
举报