title: Sequelize Query Execution In Loop
body: <p>Below iam calling addUpdateDailyLeads with an array like </p>
  
  <pre><code>[{
      "yyyymmdd": "20191124",
      "admin_login":"rasheed.s",
      "category":"PO",
      "amount":10,
      "office_id":10000,
      "new_leads_attempted":10
  },
  {
      "yyyymmdd": "20191124",
      "admin_login":"rasheed.s",
      "category":"PO",
      "amount":10,
      "office_id":10000,
      "new_leads_attempted":10
  },
  {
      "yyyymmdd": "20191125",
      "admin_login":"prajeed.av",
      "category":"FHL",
      "amount":10,
      "office_id":10000,
      "new_leads_attempted":10
  }
  ]
  
  </code></pre>
  
  <p>So,key 0 should insert,
  key 1 should update because duplicate key constratint,
  key 2 will insert,</p>
  
  <p>but im getting duplicate key constraint error on key 1,because array map not waiting for the query to be executed .</p>
  
  <pre><code>const addUpdateDailyLeads = async (req, res) =&gt; {
    let admin_login,category,office_id,new_leads_attempted,yyyymmdd,where,values;
    let data = req.body;
  
    req.body.map(async function(item,i){
      admin_login = item.admin_login,
      category = item.category,
      office_id = item.office_id,
      new_leads_attempted = item.new_leads_attempted,
      yyyymmdd    = item.yyyymmdd;
      where = {yyyymmdd:yyyymmdd, admin_login:admin_login, category:category};
      values = {yyyymmdd:yyyymmdd, admin_login:admin_login, category:category,office_id:office_id,new_leads_attempted:new_leads_attempted,update_date:moment().format('YYYYMMDDHHmmss')};
      console.log("calling  ",i);
      let chck = await addUpdateDailyLeadsCollection({where:where,values:values})
      console.log("")
      console.log("called")
    })
  
    res.json({ code: '200', message: `Advisor Daily Leads Updated ${admin_login}` });
  
  }
  
  
  const addUpdateDailyLeadsCollection = async data =&gt; {
    let transaction;    
    let where = data.where
    let values = data.values
    var Sequelize = require("sequelize");
    console.log("startef 1");
    await AdvisorLeads.findOne({ where: where }, { useMaster: true }).then( async(data)=&gt;{
      console.log("waited");
      if(data){
            await data.update({new_leads_attempted: Sequelize.literal('new_leads_attempted + '+values.new_leads_attempted)}).then(data=&gt;{
             console.log("updated")
             return Promise.resolve(1);
           })
      }else{
          AdvisorLeads.create(values).then(data=&gt;{
             console.log("inserted")
             return Promise.resolve(1);
          })
      }
    })
  
  
  };
  </code></pre>
  
  <p>final output on console </p>
  
  <pre><code>calling   0
  startef 1
  waiting 1
  calling   1
  startef 1
  waiting 1
  calling   2
  startef 1
  waiting 1
  waited
  waited
  waited
  called
  
  called
  
  called
  
  inserted
  inserted
  </code></pre>
  
  <pre><code>My expected output like
  calling   0
  startef 1
  waiting 1
  waited
  inserted
  called
  
  calling   1
  startef 1
  waiting 1
  waited 
  updated
  called
  
  calling   2
  startef 1
  waiting 1
  waited
  inserted
  called
  
  
  </code></pre>
  
  <p>Finally whait i need is to wait for each item ,execute all queries and then process next item </p>