Skip to content

Instantly share code, notes, and snippets.

@andijakl
Created October 8, 2018 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andijakl/de2df722cd1d009b03019f49abcbacb2 to your computer and use it in GitHub Desktop.
Save andijakl/de2df722cd1d009b03019f49abcbacb2 to your computer and use it in GitHub Desktop.
Node.js chained HTTP requests using promises through the request-promise-native module
const rp = require("request-promise-native");
rp("https://www.andreasjakl.com/")
.then(function(response) {
// Start second request, e.g., based on results of first request
console.log("Response length: ", response.length);
if (response.length > 100) {
return rp("https://www.andreasjakl.com/wp-content/uploads/2018/08/arcore-anchors.gif");
}
})
.then(function(response2) {
// Process response of 2nd request
console.log("Data: ", response2.substring(0,10));
})
.catch(function(error) {
console.log("Error: ", error);
});
console.log("Starting...");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment