title: How to call promise inside forEach?
body: <p>I have events flowing to below program, so based event value i trigger different promises so i wanted to check if its good idea to use promises inside forEach.</p>
  
  <p>With below code when i have <code>element.variables.caseIdFound</code> value in event it never get satisfied the condition. Any idea what is implemented wrong i am fairly new to the promises. Any example with below code highly appreciated. </p>
  
  <p>camunda.js</p>
  
  <pre><code> var caseIdFound;
      var processingCompleted;
  
      function checkTicketNum(element) {
          var EventCasesID;
          var event;
          var ticketNumber;
          var CasesID;
          var insertIDBEvents = [];
          var event;
          return new Promise(function(resolve, reject) {
                event = JSON.parse(element.variables.event.value);
                ticketNumber = event.body.raw.tkt;
                CasesID = event.body.raw.CasesIDuuid;
                  controller.insertCase(ticketNumber, function(err, response) {
                      event.body.raw.LogIDuuid = generateUUID();
                      if (response.length == 0) {
                          completeTask('TicketNotFOund',element.id);
                      } else {
                          EventCasesID = response[0].CasesID;
                          if(CasesID === EventCasesID) {
                              caseIdFound = true;
                              completeTask(element.id,caseIdFound);
                              processingCompleted  = true;
                              resolve(processingCompleted);
                           }
  
                      }
                  })
          });
  
  
      }
  
  function postIDBCall(element) {
      var event;
      return new Promise(
          function(resolve, reject) {
               event = JSON.parse(element.variables.event.value);
               controller.insertTicketAndCase2(event.body.raw, function(err, response2) {
                controller.insertTicketAndCase(event.body.raw, function(err, response1) {
                  completeTask(event.id);
                  console.log("InsertIDB Result Completed",element.id);
                  processingCompleted = true;
                   resolve(processingCompleted);
                })
            })
  
    });
  }
  
  
      module.exports = {
          checkTicketNum: checkTicketNum,
          generateUUID: generateUUID,
          completeTask: completeTask
      };
  </code></pre>
  
  <p>promise.js</p>
  
  <pre><code> var camunda = require('./camunda');
  
       data.forEach(function(element) {
           if (!element.variables.caseIdFound) {
               camunda.checkTicketNum(element).then(function(processingCompleted) {
                   console.log('1st Box', processingCompleted);
               });
           }else if(element.variables.caseIdFound) {
                        console.log('END BOX IF', element.variables.caseIdFound);
                        camunda.postIDBCall(element).then(function(processingCompleted){
                              console.log('2nd Box', processingCompleted);
                        });
                      }
       });
  </code></pre>