Created
October 7, 2018 18:47
-
-
Save kenanhancer/73fd13927283481fa1309dd4f5dad177 to your computer and use it in GitHub Desktop.
AsynchronousWithPromiseDemo2 created by kenanhancer - https://repl.it/@kenanhancer/AsynchronousWithPromiseDemo2
This file contains hidden or 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 request = require('request'); | |
let gitUserProfileDetails; | |
const delay = (duration) => new Promise(resolve => setTimeout(resolve, duration)); | |
const getData = (url) => { | |
return new Promise((resolve, reject) => { | |
const options = { | |
url: url, | |
headers: { | |
'User-Agent': 'request' | |
} | |
}; | |
request(options, (error, response, body) => { | |
if (error) { | |
reject(error); | |
} | |
else { | |
resolve(body); | |
} | |
}); | |
}); | |
}; | |
const main = () => { | |
const gitUserProfileUrl = 'https://api.github.com/users/kenanhancer'; | |
const gitUserProfilePromise = getData(gitUserProfileUrl); | |
gitUserProfilePromise | |
.then(JSON.parse) | |
.then((result) => { | |
gitUserProfileDetails = result; | |
const { name, blog, location, ...rest } = gitUserProfileDetails; | |
console.log({ name, blog, location }); | |
console.log(); | |
const followersUrlPromise = getData(gitUserProfileDetails.followers_url).then(JSON.parse); | |
return followersUrlPromise; | |
}) | |
.then((result) => { | |
console.log(`Followers of ${gitUserProfileDetails.name}:`); | |
for (let i = 0; i < result.length; i++) { | |
delay(1000 * i) | |
.then((a) => { | |
console.log((i + 1) + '-', result[i].login); | |
}); | |
} | |
}) | |
.catch(console.log); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment