Reputation: 2647
I want to download a html page & forward the body part of it to the next function. I have used step to serialize the functions. I am using request module for downloading the page.
var Step = require('step');
var request = require('request');
Step(
function getHtml() {
if (err) throw err;
var url = "my url here";
request(url, function (error, response, html) {
// i want to pass the html object to the next function view
});
},
function view(err, html) {
if (err) throw err;
console.log(html);
}
);
If I do request(url, this)
then it is passing the whole page data(response & html) to the next function.
How do I change the above code to only pass the html to next function?
Upvotes: 3
Views: 272
Reputation: 6433
Remember from the Step documentation:
It accepts any number of functions as arguments and runs them in serial order using the passed in this context as the callback to the next step.
So when each step gets called, this
is your callback to the next step. But, you're entering a callback with your request
call, so this
will change by that point. So, we just cache it.
var Step = require('step');
var request = require('request');
Step(
function getHtml() {
//if (err) throw err; <----- this line was causing errors
var url = "my url here"
, that = this // <----- storing reference to current this in closure
request(url, function (error, response, html) {
// i want to pass the html object to the next function view
that(error,html) // <----- magic sauce
});
},
function view(err, html) {
if (err) throw err;
console.log(html);
}
);
My additions are on the lines with "<------". Happy coding!
Upvotes: 1