title: Can I call a promise (with a loop) inside another promise?
body: <p>I'm trying to consume 2 services from Ebay API:
  <code>findItemsByKeywords(keyword)</code>: return a list of items(object);
  <code>getItemTransactions(itemId)</code>: return a list of item's transactions(object);</p>
  
  <p>After calling <code>findItemsByKeywords</code> and get the returned items list, I need to call <code>getItemTransactions</code> and get the transactions for each one of that items and create a new list with them.</p>
  
  <p>I'm using Node.js, Express, Ejs and MongoDB</p>
  
  <p>My code works for one interaction only. </p>
  
  <pre><code>Error:
  Error: unable to connect to database at mongodb://localhost/simplysell-development
      at NativeConnection.&lt;anonymous&gt; (/Users/guilhermefalcao/workspace/simplysell/app.js:11:9)
      at emitOne (events.js:96:13)
      at NativeConnection.emit (events.js:188:7)
      at Db.&lt;anonymous&gt; (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:169:10)
      at emitTwo (events.js:106:13)
      at Db.emit (events.js:191:7)
      at Server.listener (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongodb/lib/db.js:1786:14)
      at emitOne (events.js:96:13)
      at Server.emit (events.js:188:7)
      at Server.&lt;anonymous&gt; (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongodb/lib/server.js:274:14)
      at emitOne (events.js:96:13)
      at Server.emit (events.js:188:7)
      at Pool.&lt;anonymous&gt; (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongodb-core/lib/topologies/server.js:334:12)
      at emitOne (events.js:96:13)
      at Pool.emit (events.js:188:7)
      at Connection.&lt;anonymous&gt; (/Users/guilhermefalcao/workspace/simplysell/node_modules/mongodb-core/lib/connection/pool.js:270:12)
  </code></pre>
  
  <p>I'm not trying to access the database, I don't know why this error.</p>
  
  <p>I'm sorry about the code and english quality, <strong><em>I'm starting now</em></strong>.</p>
  
  <p>Here is some code:</p>
  
  <pre><code>router.get('/bestsellers', (req, res, next) =&gt; {
    var i = 0;
    var findItemsByKeywordsPromise = findItemsByKeywords('iphone');
    findItemsByKeywordsPromise.then(items =&gt; {
      var getItemTransactionsPromise = getItemTransactions(items);
      return getItemTransactionsPromise;
    }).then(test =&gt; console.log(test))
  
  });
  
  function getItemTransactions(items) {
  
    return new Promise((resolve, reject) =&gt; {
      var list = [];
      items.forEach(function (item) {
  
        ebay.xmlRequest({
          serviceName: 'Trading',
          opType: 'GetItemTransactions',
  
          // app/environment
          devId: 'xxxxxxx',
          certId: 'xxxxxxx',
          appId: 'xxxxxxx',
          sandbox: false,
  
          // per user
          authToken: 'xxxxxxx',
  
          params: {
            'ItemID': item.itemId,
            'NumberOfDays': 30
          }
        }, function (error, results) {
          list.push(results)
          resolve(list);
        });
      })
    });
  
  }
  
  function findItemsByKeywords(keywords) {
  
    return new Promise((resolve, reject) =&gt; {
  
      var params = {
        keywords: [keywords],
      };
  
      ebay.xmlRequest({
        serviceName: 'Finding',
        opType: 'findItemsByKeywords',
        appId: 'xxxx',      // FILL IN YOUR OWN APP KEY, GET ONE HERE: https://publisher.ebaypartnernetwork.com/PublisherToolsAPI
        params: params,
        parser: ebay.parseResponseJson    // (default)
      },
        // gets all the items together in a merged array
        function itemsCallback(error, itemsResponse) {
          if (error) reject(error);
  
          console.log('findItemsByKeywords called');
  
          var items = itemsResponse.searchResult.item;
  
          console.log('Found', items.length, 'items');
          resolve(items);
        }
      );
    });
  }
  </code></pre>