Created
May 24, 2017 20:44
-
-
Save AdilKhn/0084eefc434b410844dfb4bbdfe977c8 to your computer and use it in GitHub Desktop.
Gist Describing rxjs with promise.
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 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