title: pg-promise - Not resolving multiple queries
body: <p>I'm in the process of creating a mass mailer that allows a company to send out an email template to a specific plan that contains X amount of subscribers. </p>
  
  <p>However, when using pg-promise, I can't figure how to loop through an array that contains one or more unique plan names that'll then be used as query parameters for subsequent queries to gather a single array of results. <strong>The subsequent queries don't resolve before express sends back a specified error message.</strong></p>
  
  <p>For example:</p>
  
  <pre><code>const selectedTemplate = await db.oneOrNone("SELECT * FROM templates WHERE userid=$1 and uniqueTemplateName=$2", [req.session.id, req.body.uniquetemplatename])
  </code></pre>
  
  <p>returns a JSON object... </p>
  
  <pre><code>{ ...
    plans : [ 'Unique Plan 1', 'Unique Plan 2', 'Unique Plan 3'...],
    ...
  }
  </code></pre>
  
  <p>I then want to loop through this <code>plans</code> array to gather some subscriber emails. Since the selectedTemplate's <code>plans</code> array can contain a single "Unique Plan" or multiple, I'm trying to figure out how to get this query to work:</p>
  
  <pre><code>let subscriberEmails = [];
  
  await each(selectedTemplate.plans, async plan =&gt; {
    const emails = await db.any("SELECT email FROM subscribers WHERE userid=$1 AND planName=$2", [req.session.id, plan])          
    each(emails, ({email}) =&gt; subscriberEmails.push(email))           
    console.log('Found emails: ', subscriberEmails);
  })
  
  console.log('Selected emails: ', subscriberEmails);
  res.status(500).json({ message: "Route not configured yet" })
  </code></pre>
  
  <p><strong>The problem I'm having is that Express isn't waiting for the pg-promise to resolve.</strong> So it's triggering my 500 server "Route not configured yet" message, <strong>then</strong> resolves the queries:</p>
  
  <pre><code>14:48:33 SELECT * FROM templates WHERE userid='12345' and uniqueTemplateName='example-unique-template'
  
          selectedTemplate: anonymous {
            id: '84efa448-b149-11e8-a7fd-3b9e4e9e5ece',
            key: 9,
            userid: '12345',
            status: 'active',
            fromsender: 'example@helpdesk.com',
            subject: 'Thank you for using our services!',
            templatename: 'Example Unique Template',
            uniquetemplatename: 'example-unique-template',
            message: '&lt;h3&gt;This is a test!&lt;/h3&gt;',
            plans: [ 'Unique Plan 1', 'Unique Plan 2' ] 
          }
  
          Selected emails: []
  
  POST /api/messages/create 500 28 - 34.125 ms
  
  14:48:33 SELECT email FROM subscribers WHERE userid='12345' AND planName='Unique Plan 1'
  
          Found emails:  [ 
           'betatester19@example.com',
           'betatester20@example.com',
           'betatester21@example.com'
          ]
  
  14:48:34 SELECT email FROM subscribers WHERE userid='12345' AND planName='Unique Plan 2'
  
          Found emails:  [ 
           'betatester19@example.com',
           'betatester20@example.com',
           'betatester21@example.com',
           'betatester1@example.com',
           'betatester2@example.com',
           'betatester3@example.com',
           'betatester4@example.com',
           'betatester5@example.com',
           'betatester6@example.com',
           'betatester7@example.com',
           'betatester8@example.com',
           'betatester9@example.com',
           'betatester10@example.com',
           'betatester11@example.com',
           'betatester12@example.com',
           'betatester13@example.com',
           'betatester14@example.com',
           'betatester15@example.com',
           'betatester16@example.com',
           'betatester17@example.com',
           'betatester18@example.com' 
          ]
  </code></pre>
  
  <p>The function as is:</p>
  
  <pre><code>async (req, res, done) =&gt; {
      if (!req.body) {
        return res.status(404).json({ message: "Missing query parameters" });
        done();
      }
  
      try {
        const selectedTemplate = await db.oneOrNone("SELECT * FROM templates WHERE userid=$1 and uniqueTemplateName=$2", [req.session.id, uniquetemplatename]);
        if (!selectedTemplate) {
          return res.status(404).json({ message: "Unable to locate selected template" });
          done();
        }
        console.log('selectedTemplate', selectedTemplate);
  
        let subscriberEmails = [];
        await each(selectedTemplate.plans, async plan =&gt; {
          const emails = await db.any("SELECT email FROM subscribers WHERE userid=$1 AND planName=$2", [req.session.id, plan])
          each(emails, ({email}) =&gt; subscriberEmails.push(email))
          console.log('Found emails: ', subscriberEmails);
        })
  
        console.log('Selected emails: ', subscriberEmails); 
        res.status(500).json({ message: "Route not configured yet" })
      } catch (err) { res.status(500).json({ message: err }); done(); }
    }
  </code></pre>
  
  <p>A vastly simplified DB structure:</p>
  
  <pre><code>├── Template
  │   └── Plans (references an array of unique plan name(s))
  |
  |── Plan
  |   └── PlanName (unique plan name)
  |
  │── Subscriber
  |   └── PlanName (references a single unique plan name)
  |   └── Email (unique email)
  </code></pre>
  
  <p>I've tried using a <code>db.task()</code>, but again, the queries didn't resolve before Express sent back the message.</p>