3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RxJS Observableの作成 (1)  Observable.of の使い方

Last updated at Posted at 2018-05-23

用途

Observable.ofは、シンプルな値を流したいときに利用します。
引数を可変長で受け取り、値をそのまま流します。

基本的な使い方

引数で受け取った値をそのまま流します。
入れれる値は文字列、数値、配列、オブジェクト等です。
通常単体の利用することはあまりなく、複数のオペレーターと組み合わせて使用します。

const numbers = Rx.Observable.of(1,2,3).subscribe(x => {
  console.log(x);
});

// 結果
// 1
// 2
// 3


Rx.Observable.of('hoge',[1,2,3],{name:'keisukeh'}).subscribe(x => {
  console.log(x);
});

// 結果
// hoge
// [1,2,3]
// {name:'keisukeh'}

ES6 stackblitz
TypeScript stackblitz

他のオペレーターとの組み合わせ

const numbers = Rx.Observable.of(1,2,3);
const timer = Rx.Observable.timer(2000);
Rx.Observable.concat(timer, numbers).skip(1).subscribe(x => {
  console.log(x);
})

// 結果
// (2秒後)
// 1
// 2
// 3

ES6 stackblitz
TypeScript stackblitz

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?