Rx.Observable.from(iterable, [mapFn], [thisArg], [scheduler])
从Array创建一个Observable,参数也可以是一个类数组的对象,一个Promise,一个可迭代对象或一个Observable类对象。
参数
iterable
(Array
|Arguments
|Iterable
): 类数组或可迭代对象[mapFn]
(Function
): 应用到第一个参数的映射函数[thisArg]
(Any
): 提供映射函数上下文[scheduler=Rx.Scheduler.currentThread]
(Scheduler
): 枚举输入序列的调度器
返回值
(Observable
): 通过给定的可迭代序列生成的observable
.
例
Array-like object (arguments) to Observable
function f() {
return Rx.Observable.from(arguments);
}
f(1, 2, 3).subscribe(
x => console.log(`onNext: ${x}`),
e => console.log(`onError: ${e}`),
() => console.log('onCompleted'));
// => onNext: 1
// => onNext: 2
// => onNext: 3
// => onCompleted
Set
var s = new Set(["foo", window]);
Rx.Observable.from(s).subscribe(
x => console.log(`onNext: ${x}`),
e => console.log(`onError: ${e}`),
() => console.log('onCompleted'));
// => onNext: foo
// => onNext: window
// => onCompleted
Map
var m = new Map([[1, 2], [2, 4], [4, 8]]);
Rx.Observable.from(m).subscribe(
x => console.log(`onNext: ${x}`),
e => console.log(`onError: ${e}`),
() => console.log('onCompleted'));
// => onNext: [1, 2]
// => onNext: [2, 4]
// => onNext: [4, 8]
// => onCompleted
String
Rx.Observable.from("foo").subscribe(
x => console.log(`onNext: ${x}`),
e => console.log(`onError: ${e}`),
() => console.log('onCompleted'));
// => onNext: f
// => onNext: o
// => onNext: o
// => onCompleted
Array
Rx.Observable.from([1, 2, 3], x => x + x).subscribe(
x => console.log(`onNext: ${x}`),
e => console.log(`onError: ${e}`),
() => console.log('onCompleted'));
// => onNext: 2
// => onNext: 4
// => onNext: 6
// => onCompleted
Generate a sequence of numbers
Rx.Observable.from({length: 5}, (v, k) => k).subscribe(
x => console.log(`onNext: ${x}`),
e => console.log(`onError: ${e}`),
() => console.log('onCompleted'));
// => onNext: 0
// => onNext: 1
// => onNext: 2
// => onNext: 3
// => onNext: 4
// => onCompleted