etc

rxjs

paulaner80 2018. 12. 21. 19:17
반응형
const { range, from, of, Observable } = require("rxjs");
const { map, filter, concat } = require("rxjs/operators");
const fs = require("fs");

// range(1, 200)
// .pipe(
// filter(x => x % 2 === 1),
// map(x => x + x)
// )
// .subscribe(x => console.log(x));

function delay() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
// resolve("delayed");
resolve();
}, 1000);
});
}

function writeFile() {
return new Promise(function(resolve, reject) {
var content = '[{ "name": "LuiBei", "birth": "161", "death": "223", "hometown": "탁현" }, { "name": "GuanYu", "birth": "161", "death": "219", "hometown": "상평" }, { "name": "ZhangFei", "birth": "165", "death": "221", "hometown": "탁현" }]';
fs.writeFile("0001.txt", content, function(err) {
if (err) {
reject(err);
} else {
// resolve("saved");
resolve();
}
});
});
}

function readFile() {
return new Promise(function(resolve, reject) {
fs.readFile("0001.txt", "utf-8", function(err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
}

function myJsonParser(person) {
try {
return JSON.parse(person);
} catch (e) {
console.log(e);
return "aa";
}
}

const delay$ = from(delay());
const writeFile$ = from(writeFile());
const readFile$ = from(readFile());

const myStream$ = delay$
.pipe(concat(writeFile$))
.pipe(concat(readFile$))
.pipe(filter(res => res))
.pipe(map(person => JSON.parse(person)))
.pipe(arr => {
console.log("arr : " + JSON.stringify(arr));
return Observable.create(function(oberser) {
arr.forEach(item => {
console.log("item : " + JSON.stringify(item));
oberser.next(item);
});
});
});
// delay$.subscribe(result => console.log(result));
//writeFile$.subscribe(result => console.log(result));

// console.log(typeof concat);

myStream$.subscribe(result => console.log(result));

// const sourceOne = of(1, 2, 3);
// const sourceTwo = of(4, 5, 6);

// const example = sourceOne.pipe(concat(sourceTwo));
// const subscribe = example.subscribe(val => console.log(" Example Basic Concat : " + val));


'etc' 카테고리의 다른 글

php cheat sheet  (0) 2019.04.25
lck 2019 서머 일정표  (0) 2019.02.25
다음 팟플레이어 마지막 버전(1.6.63891)  (0) 2017.11.06
youtube-dl 다운로드 방법  (0) 2017.09.25
BC 4 for windows. 30일이 초기화 방법  (2) 2017.09.12