javascript - Return value from Request -
javascript - Return value from Request -
i new in node.js , javasript. possible homecoming value request? thanks
var request = require('request'); //parse text function(text) { request(text, function (error, response, body) { if (!error && response.statuscode == 200) { var $ = cheerio.load(body); //get title var title = $("title").text(); }) need title here };
to need understand async nature of node, @ moment title might not available, since request title async, have wait response come before can access it
you can this
function gettitle(uri, callback) { request(uri, function (error, response, body) { if (error || response.statuscode != 200) { homecoming callback(error); } var $ = cheerio.load(body); //get title homecoming callback(null, $("title").text()); }); } gettitle(uri, function(err, title) { //access title here });
javascript node.js
Comments
Post a Comment