javascript - How can I make this request synchronous in node.js -
here asynchronous code snippet working giving website urls asynchronous request want make synchronous request can me here using alchemy api feeds cloud fetching data
var urls = [website ur names] for(var j=0;j<urls.length;j++){ alchemyapi.feeds("url",urls[j], {}, function(response) { console.log(response["feeds"]); for(var i=0;i<response["feeds"].length;i++) var feed = response["feeds"][i].feed; getfeed(feed); console.log("title: " +objtostring(response["feeds"][i])); }); } function objtostring (obj) { var str = ''; (var p in obj) { if (obj.hasownproperty(p)) { str += p + '::' + obj[p] + '\n'; } } return str; } var request = require('request'); var getfeed = function(feed){ feeds = encodeuricomponent(feed); request('http://cloud.example.com/v3/mixes/contents?streamid=feed/'+feeds/, function (error, response, body) { if (!error && response.statuscode == 200) { console.log(body) } else { console.log("error "+response.statuscode) } }); }
var urls = [website ur names] var request = require('request'); function promiserequest(feeds){ return new promise(function(resolve,reject){ request('http://cloud.example.com/v3/mixes/contents?streamid=feed/'+feeds/, function (error, response, body) { if (!error && response.statuscode == 200) { resolve(body); } else { reject(response.statuscode); } }); } } function getfeedsfromurls(urls){ return new promise(function(resolve,reject){ var count=0,totalcount = urls.length; var result=[]; for(var j=0;j<urls.length;j++){ alchemyapi.feeds("url",urls[j], {}, function(response) { for(var i=0;i<response["feeds"].length;i++) var feed = response["feeds"][i].feed; promiserequest(feed).then(function(data){ count++; result.push(data); if(count>=totalcount){ resolve(result); } }).catch(function(err){ count++; if(count>=totalcount){ resolve(result); } }); }); } }); } getfeedsfromurls(urls).then(function(result){ console.log(result); })
Comments
Post a Comment