Chaining promises with promises inside then() : <p>How do you chain in an scenario like this?</p>

<p><em>api</em> is a function that returns a promise after an http request. <em>auth</em> is a function that returns a promise after <em>api</em> respond, if resolves <em>api</em> is called for a second time, if not <em>auth</em> rejects.</p>

<p>I tried this, but not only am I going back to the callback hell, it does not work.</p>

<pre><code>function api(query) {
   return new Promise(function(resolve, reject) {
     //DO SOME STUFF AND SOMETIMES resolves...
   })
}

function auth() {
   return new Promise(function(resolve, reject) {
     api("/foo").then(function(asset1) {
        api("/bar").then(function(asset2) {
           resolve(asset2);
        }).catch(function() {
           reject();
        })
     }).catch(function(error) {
        reject();
     })

   })
}
</code></pre>
