Skip to content

Instantly share code, notes, and snippets.

@AdilKhn
Created May 24, 2017 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AdilKhn/0084eefc434b410844dfb4bbdfe977c8 to your computer and use it in GitHub Desktop.
Save AdilKhn/0084eefc434b410844dfb4bbdfe977c8 to your computer and use it in GitHub Desktop.
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