title: How to run SQL query with inside a javascript loop(map)?
body: <p>what I'm trying to do is:</p>
  <ul>
  <li>loop through array of objects using map function</li>
  <li>run multiple SQL queries according to object</li>
  <li>append result of queries with object</li>
  </ul>
  <p>I'm looping through offers which is array of object.I get <code>free_item</code> and <code>buy_item</code> which are associated with <code>offer</code>.I am using knexjs for postgresql database with nodejs</p>
  <p>Here's actual code:</p>
  <pre><code>offers = await Promise.all(offers.map(async offer =&gt; {
          free_item_id = await db(&quot;offer_free_items&quot;).where({&quot;offer_free_items.offer_id&quot;:offer.id}).select([&quot;item_id&quot;]).first()
          console.log(&quot;-----------------debug line 1------------------&quot;)
  
          buy_item_id = await db(&quot;offer_buy_items&quot;).where({&quot;offer_buy_items.offer_id&quot;:offer.id}).select([&quot;item_id&quot;]).first()
          console.log(&quot;-----------------debug line 2-------------------&quot;)
  
          offer[&quot;free_item_id&quot;] = get_item_id
          offer[&quot;buy_item_id&quot;] = buy_item_id
          return offer
  }))
  </code></pre>
  <p>The Problem is it is not running in correct sequence.The sequence of the output is</p>
  <ol>
  <li>debug line 1</li>
  <li>debug line 1</li>
  <li>debug line 2</li>
  <li>debug line 2</li>
  </ol>
  <p>The correct order should be like this:</p>
  <ol>
  <li>debug line 1</li>
  <li>debug line 2</li>
  <li>debug line 1</li>
  <li>debug line 2</li>
  </ol>