title: Removing nested promises
body: <p>I'm new to promises and writing network code using requests and promises in NodeJS.</p>
  
  <p>I would like to remove these nested promises and chain them instead, but I'm not sure how I'd go about it/whether it is the right way to go.</p>
  
  <pre><code>exports.viewFile = function(req, res) {
  var fileId = req.params.id;
  boxContentRequest('files/' + fileId + '/content', req.user.box.accessToken)
      .then(function(response) {
          boxViewerRequest('documents', {url: response.request.href}, 'POST')
              .then(function(response) {
                  boxViewerRequest('sessions', {document_id: response.body.id}, 'POST')
                      .then(function(response) {
                          console.log(response);
                      });
              });
      });
  };
  </code></pre>
  
  <p>This is the request code:</p>
  
  <pre><code>var baseContentURL = 'https://api.box.com/2.0/';
  var baseViewerURL = 'https://view-api.box.com/1/';
  
  function boxContentRequest(url, accessToken) {
      return new Promise(function (resolve, reject) {
              var options = {
                  url: baseContentURL + url,
                  headers: {
                      Authorization: 'Bearer ' + accessToken,
                  }
              };
        request(options, function (err, res) {
          if (err) {
            return reject(err);
          } else if (res.statusCode !== 200) {
            err = new Error("Unexpected status code: " + res.statusCode);
            err.res = res;
            return reject(err);
          }
          resolve(res);
        });
      });
  }
  
  function boxViewerRequest(url, body, method) {
      return new Promise(function (resolve, reject) {
              var options = {
                  method: method,
                  url: baseViewerURL + url,
                  headers: {
                      Authorization: 'Token ' + config.box.viewerApiKey
                  },
                  json: body
              };
        request(options, function (err, res, body) {
          if (err) {
            return reject(err);
          } else if (res.statusCode !== 200 &amp;&amp; res.statusCode !== 201 &amp;&amp; res.statusCode !== 202) {
            err = new Error("Unexpected status code: " + res.statusCode);
            err.res = res;
            return reject(err);
          }
          resolve(res, body);
        });
      });
  }
  </code></pre>
  
  <p>Any insight would be appreciated.</p>