title: nodejs array of promises, how can I do this?
body: <p>I am very new to async programming and have never done a queue of promises before, so I have no idea how to solve this.</p>
  
  <p>I have a table with bank-accounts</p>
  
  <p>for each bank account I have a list of receipts</p>
  
  <pre><code>Account 111
   - Receipt 001
   - Receipt 002
   - Receipt 003
  
  Account 222
   - Receipt 004
   - Receipt 005
   - Receipt 006
  </code></pre>
  
  <p>So I set up a promise that find() all the bank accounts.</p>
  
  <p>And then I loop thru all bank accounts, and for each account I find all receipts.</p>
  
  <p>What should I do?  Create a promise for each receipt find()? </p>
  
  <p>Create an array of promises?  (how do you do that btw)</p>
  
  <p>Or is there a third option?</p>
  
  <pre><code>// 
  // Find all bank accounts
  // 
  var findBank = new Promise(
      (resolve, reject) =&gt; {
      bankTable.find({}
      ,function(err, data) {
          if (!err) {
              resolve(data);
          } else {
              reject(new Error('findBank ERROR : ' + err));
          }
      });
  });
  
  
  // 
  // Find the RECEIPTS for each bank account
  // 
  var findAllReceipts = function(accountArray) {
  
      for (var i=0; i&lt;accountArray.length; i++) {
  
          var findReceipt = new Promise(
              (resolve, reject) =&gt; {
              receiptTable.find(
                  { accountNo: accountArray[i].accountNo } 
              ,function(err, data) {
                  if (!err) {
                      resolve(data);
                  } else {
                      reject(new Error('findPrice ERROR : ' + err));
                  }
              });
          });
      }
  }
  
  // 
  // Run the promises
  // 
  findBank
      .then(findAllReceipts)
      .catch(err =&gt; {
          console.log("getbankAccountReport ERR: " + err);
          res.json({error:true,err})
      })
  </code></pre>