Created
October 8, 2018 12:11
-
-
Save kenanhancer/91de00cf78a95c34bbc5e8eb28d2f2e9 to your computer and use it in GitHub Desktop.
AsynchronousWithAsyncAwaitPromiseDemo3 created by kenanhancer - https://repl.it/@kenanhancer/AsynchronousWithAsyncAwaitPromiseDemo3
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 = async () => { | |
const gitUserProfileUrl = 'https://api.github.com/users/kenanhancer'; | |
const gitUserProfileBody = await getData(gitUserProfileUrl); | |
gitUserProfileDetails = JSON.parse(gitUserProfileBody); | |
const { name, blog, location, ...rest } = gitUserProfileDetails; | |
console.log({ name, blog, location }); | |
console.log(); | |
const followersUrlBody = await getData(gitUserProfileDetails.followers_url); | |
const followers = JSON.parse(followersUrlBody); | |
console.log(`Followers of ${gitUserProfileDetails.name}:`); | |
for (let i = 0; i < followers.length; i++) { | |
await delay(1000 * i); | |
console.log((i + 1) + '-', followers[i].login); | |
} | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment