Skip to content

Instantly share code, notes, and snippets.

@koyanloshe
Last active June 15, 2019 08:18
Show Gist options
  • Save koyanloshe/f65cbb6ca9b78a3770c50445cb9582c0 to your computer and use it in GitHub Desktop.
Save koyanloshe/f65cbb6ca9b78a3770c50445cb9582c0 to your computer and use it in GitHub Desktop.
Async request example #Javascript
var request = require("request");
var userDetails;
function getData(url) {
// Setting URL and headers for request
var options = {
url: url,
headers: {
'User-Agent': 'request'
}
};
// Return new promise
return new Promise(function(resolve, reject) {
// Do async job
request.get(options, function(err, resp, body) {
if (err) {
reject(err);
} else {
resolve(body);
}
})
})
}
var errHandler = function(err) {
console.log(err);
}
function main() {
var userProfileURL = "https://api.github.com/users/narenaryan";
var dataPromise = getData(userProfileURL);
// Get user details after that get followers from URL
dataPromise.then(JSON.parse, errHandler)
.then(function(result) {
userDetails = result;
// Do one more async operation here
var anotherPromise = getData(userDetails.followers_url).then(JSON.parse);
return anotherPromise;
}, errHandler)
.then(function(data) {
console.log(data)
}, errHandler);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment