Instantly share code, notes, and snippets.

Embed
What would you like to do?
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