title: concrete example for Promises and nodejs (and no solution)
body: <p>So I'd like to learn more about Promises (I arbitrarily chose Bluebird, despite the 'succinct' documentation) by attempting to implement a common use case, using nodejs:</p>
  
  <p>I have a bunch of some 9000 or so URLs in a file, with some empty lines as well. I would like to:</p>
  
  <ul>
  <li>FILTER out empty lines (url.length &lt;= 0)</li>
  <li>FILTER out those that no longer respond (via a HEAD request using the <code>request</code> module)</li>
  <li>MAP to get an IPv4 address via <code>dns.resolve()</code></li>
  <li>MAP use that IPv4 address and <a href="http://ipinfo.io/xx.xx.xx.xx/geo" rel="nofollow">http://ipinfo.io/xx.xx.xx.xx/geo</a> to get geo data (ok, yes, there is a daily API limit, but let's assume I could)</li>
  <li>write the resulting information as an array of JSON objects in a new file</li>
  <li>and of course profit from the fact that this will run in parallel and therefore be much faster than doing it sequentially</li>
  </ul>
  
  <p>The first filter is easy as it's returning immediately (here using Bluebird):</p>
  
  <p></p>
  
  <pre><code>Promise.each(urls, function(value, index, length) {
    return value.length &gt; 0;
  }).then(
    console.log(urls);
  );
  </code></pre>
  
  <p></p>
  
  <p>But how can I feed back the result of an asynchronous head request back to the Promise?  Here is another, more complete example of where I'm hitting a wall (see the comments inline):</p>
  
  <pre><code>&lt;pre class="prettyprint lang-js"&gt;
  
  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 &gt; 0);
  }
  
  var checkHead = function(url) {
      return Promise.resolve(
        // ??? seee below for an 'unPromised' function that works on its own
      )
  }
  
  var logit = function(value) {
    console.log((urls.length - value.length) + " empty line(s)");
  }
  
  Promise
    .filter(urls, checkLength)
    // and here? .filter(urls, checkHead) ? I don't think this would work.
    // and I haven't even look at the map functions yet, although I guess
    // that once I've understood the basic filter, map should be similar.
    .then(logit);
  
  &lt;/pre&gt;
  </code></pre>
  
  <p>For the <code>checkHead</code> function I planned to modify something like this:</p>
  
  <pre><code>var isURLvalid = function(url) {
    request
      .head(url)
      .on('response', function(response) {
        console.log("GOT SUCCESS: " + response);
        callback(response.statusCode &lt; 300);
      })
      .on('error', function(error) {
        console.log("GOT ERROR: " + response);
        callback(false);
      })
  };
  </code></pre>
  
  <p>Without wanting to complain, I'm still desperately looking for some good tutorial introductory material that takes the developer who's unfamiliar with Promises by the hand and shows practical implementations of common use cases, cookbook-like. If there is, I'd be glad to get some pointers.</p>