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