b2977053
2/26/2020 - 2:50 PM

RxJS Creation Operator from

//Creation Operator - from 輸入陣列 or 字串 or Promise 物件

var arr = ['Jerry', 'Anna', 2016, 2017, '30 days'] 
var source = Rx.Observable.from(arr);

source.subscribe({
    next: function(value) {
        console.log(value)
    },
    complete: function() {
        console.log('complete!');
    },
    error: function(error) {
        console.log(error)
    }
});

// Jerry
// Anna
// 2016
// 2017
// 30 days
// complete!
//------------------------

var source = Rx.Observable.from('Word');
source.subscribe(console.log);
// W
// o
// r
// d

//Promise 物件
//(這裡也可以用 fromPromise ,會有相同的結果。)
var source = Rx.Observable.from(new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve('Hello RxJS!');
    },3000)
  }))

//promise 
https://cythilya.github.io/2018/10/31/promise/