javascript - concrete example for Promises and nodejs (and no solution) -


so i'd learn more promises (i arbitrarily chose bluebird, despite 'succinct' documentation) attempting implement common use case, using nodejs:

i have bunch of 9000 or urls in file, empty lines well. to:

  • filter out empty lines (url.length <= 0)
  • filter out no longer respond (via head request using request module)
  • map ipv4 address via dns.resolve()
  • map use ipv4 address , http://ipinfo.io/xx.xx.xx.xx/geo geo data (ok, yes, there daily api limit, let's assume could)
  • write resulting information array of json objects in new file
  • and of course profit fact run in parallel , therefore faster doing sequentially

the first filter easy it's returning (here using bluebird):

promise.each(urls, function(value, index, length) {   return value.length > 0; }).then(   console.log(urls); ); 

but how can feed result of asynchronous head request promise? here another, more complete example of i'm hitting wall (see comments inline):

<pre class="prettyprint lang-js">  var promise = require('bluebird'),   request = promise.promisifyall(require('request'));  var urls = ["", "https://google.com/", "http://www.nonexistent.url"];  var checklength = function(url) {     return promise.resolve(url.length > 0); }  var checkhead = function(url) {     return promise.resolve(       // ??? seee below 'unpromised' function works on own     ) }  var logit = function(value) {   console.log((urls.length - value.length) + " empty line(s)"); }  promise   .filter(urls, checklength)   // , here? .filter(urls, checkhead) ? don't think work.   // , haven't @ map functions yet, although guess   // once i've understood basic filter, map should similar.   .then(logit);  </pre> 

for checkhead function planned modify this:

var isurlvalid = function(url) {   request     .head(url)     .on('response', function(response) {       console.log("got success: " + response);       callback(response.statuscode < 300);     })     .on('error', function(error) {       console.log("got error: " + response);       callback(false);     }) }; 

without wanting complain, i'm still desperately looking tutorial introductory material takes developer who's unfamiliar promises hand , shows practical implementations of common use cases, cookbook-like. if there is, i'd glad pointers.

the filter , map functions works default ones javascript array.

i've used request promises before , there specific module called request-promise. might more convenient.

i feel request-promise module great show how promises great.

instead of falling callback hell each added request go deeper instead promises

rp(login) .then(function(body) {     return rp(profile); }) .then(function(body) {     return rp(profile_settings); }) .then(function(body) {     return rp(logout); }) .catch(function(err) {     // procedure failed     console.error(err); }); 

i rewrote current code this

var promise = require("bluebird"); var rp = require("request-promise");  var urls = ["", "https://google.com/", "http://www.nonexistent.url"];  promise     // checklength     .filter(urls, function(url){return url.length > 0})     .then(function(list) {         // logit         console.log((urls.length - list.length) + " empty line(s)");          // checkhead         return promise.filter(list, function(url) {             // using request-promise , getting full response             return rp.head({ uri: url, resolvewithfullresponse: true })                 .promise().then(function(response) {                     // statuscode 200 ok                     return response.statuscode === 200;                 })                 .catch(function(){                     // other statuscodes incl.                     // errorcodes considered invalid                     return false;                 });         })     })     .then(console.log); 

Comments

Popular posts from this blog

how to insert data php javascript mysql with multiple array session 2 -

multithreading - Exception in Application constructor -

windows - CertCreateCertificateContext returns CRYPT_E_ASN1_BADTAG / 8009310b -