Created
January 4, 2016 01:05
-
-
Save dotfold/59dedb6ab212c642f29c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const session$ = function (key) { | |
const request = request('GET', 'YOUR_URL') | |
return Rx.Observable.create(observer) { | |
req.on('response', (res) => { | |
// check status code | |
// obs.onError() if not 200 | |
// else... | |
res.on('end', () => { | |
let result = '' | |
res.on('data', (chunk) => { | |
result += chunk | |
}) | |
const json = JSON.parse(result) | |
observer.onNext(json) | |
observer.onCompleted() | |
}) | |
}) | |
} | |
} | |
const data$ = function (token) { | |
const request = request('GET', 'YOUR_URL') | |
return Rx.Observable.create(observer) { | |
req.on('response', (res) => { | |
// check status code | |
// obs.onError() if not 200 | |
// else... | |
res.on('end', () => { | |
let result = '' | |
res.on('data', (chunk) => { | |
result += chunk | |
}) | |
const json = JSON.parse(result) | |
observer.onNext(json) | |
observer.onCompleted() | |
}) | |
}) | |
} | |
} | |
const combinedResult = session$(key) | |
.flatMap((sessionResult) => { | |
console.log(`Session Token: ${sessionResult}`); | |
// use this for your next call.... | |
// you probably want to transform your data ? | |
const dataSignal$ = data$(sessionResult) | |
.map((data) => { | |
return { | |
title: data.title | |
} | |
}) | |
return dataSignal$ | |
}) | |
.subscribe( | |
(dataResult) => console.log(`data: ${dataResult}`); | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment