JS promise passing between functions / wait for promises
<p>I am trying to work through JS Promises in node.js and don't get the solution for passing promises between different function.</p>

<p><strong>The task</strong></p>

<p>For a main logic, I need to get a json object of <em>items</em> from a REST API. The API handling itself is located in a <code>api.js</code> file.</p>

<p>The request to the API inthere is made through the <a href="https://www.npmjs.com/package/request-promise" rel="nofollow">request-promise</a> module. I have a private <code>makeRequest</code> function and public helper functions, like <code>API.getItems()</code>.</p>

<p>The main logic in <code>index.js</code> needs to wait for the API function until it can be executed. </p>

<p><strong>Questions</strong></p>

<ol>
<li>The promise passing kind of works, but I am not sure if this is more than a coincidence. Is it correct to return a Promise which returns the responses in <code>makeRequest</code>?</li>
<li>Do I really need all the promises to make the main logic work only after waiting for the items to be setup? Is there a simpler way?</li>
<li>I still need to figure out, how to best handle errors from a) the <code>makeRequest</code> and b) the <code>getItems</code> functions. What's the best practice with Promises therefor? Passing Error objects?</li>
</ol>

<p>Here is the Code that I came up with right now:</p>

<pre><code>// index.js

var API    = require('./lib/api');

var items;

function mainLogic() {

  if (items instanceof Error) {
    console.log("No items present. Stopping main logic.");
    return;
  }

  // ... do something with items
}


API.getItems().then(function (response) {
  if (response) {
    console.log(response);
    items = response;
    mainLogic();
  }
}, function (err) {
  console.log(err);
});
</code></pre>

<p>api.js</p>

<pre><code>// ./lib/api.js

var request = require('request-promise');

// constructor
var API = function () {
  var api = this;

  api.endpoint = "https://api.example.com/v1";
  //...
};

API.prototype.getItems = function () {
  var api      = this;
  var endpoint = '/items';


  return new Promise(function (resolve, reject) {
    var request = makeRequest(api, endpoint).then(function (response) {
      if (200 === response.statusCode) {
        resolve(response.body.items);
      }
    }, function (err) {
      reject(false);
    });
  });
};

function makeRequest(api, endpoint) {
  var url     = api.endpoint + endpoint;
  var options = {
    method: 'GET',
    uri: url,
    body: {},
    headers: {},
    simple: false,
    resolveWithFullResponse: true,
    json: true
  };

  return request(options)
    .then(function (response) {
      console.log(response.body);
      return response;
    })
    .catch(function (err) {
      return Error(err);
    });
}

module.exports = new API();
</code></pre>

<p>Some more background:
At first I started to make API request with the <a href="https://www.npmjs.com/package/request" rel="nofollow">request</a> module, that works with callbacks. Since these were called async, the items never made it to the main logic and I used to handle it with Promises.</p>