title: Flattening promises in javascript
body: <p>The bluebird library seems to automagically use <code>Promise::then</code> both as an equivalent of "map" and "flatMap" on the promise, eg see this example.</p>
  
  <pre><code>var Promise;
  
  Promise = require('bluebird').Promise;
  
  Promise.resolve(1).then(function(x) {
    return Promise.resolve(x + 1);
  }).then(function(x) {
    return console.log(x); // =&gt; `2` (not a promise)
  });
  
  Promise.resolve(1).then(function(x) {
    return x + 1;
  }).then(function(x) {
    return console.log(x); // =&gt; `2`
  });
  
  Promise.reject('hi').catch(function(x) {
    return Promise.reject('hi2');
  }).catch(function(x) {
    return console.error(x); //  =&gt; `hi2` (not a promise)
  });
  </code></pre>
  
  <p>Is this a contract of the es6 Promise API? I see no mention of this flattening behavior <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then" rel="noreferrer">here</a> or <a href="https://msdn.microsoft.com/en-us/library/dn802833(v=vs.94).aspx" rel="noreferrer">here</a>, for example.</p>