Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Gist Describing rxjs with promise.
const Rx = require('rxjs/Rx');
const request = require('request');
const stdin = process.openStdin();
function prompt() {
stdin.write('\n\nEnter github id: ' );
};
prompt();
const options = {
headers: {
'User-Agent': 'node'
}
}
let source$={};
const apiGetterPromise = function(username) {
return new Promise((resolve, reject) => {
options.url = `https://api.github.com/users/${username}`;
console.log('-------Making the Request--------');
console.log(`Using Url: ${JSON.stringify(options)}` );
request(options, function(error, response, body){
console.log(`STATUS: ${response.statusCode}`)
if(error || (response && response.statusCode == 404)){
reject("User Not Found!!");
}else {
resolve(body);
}
});
});
}
function printGithubData(){
source$.subscribe(
data => {
console.log('--------------Response Received----------');
let json = JSON.parse(data);
console.log(json.name);
console.log(json.blog);
console.log(json.bio);
console.log(json.public_repos);
},
err => {
console.log(`Error occurred: ${err}`);
prompt();
},
complete => {
console.log('-------Request Completed--------\n');
prompt();
}
);
}
stdin.addListener("data", function(input){
source$ = Rx.Observable.fromPromise(apiGetterPromise(input.toString().trim()));
printGithubData();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment