title: Node: Requests inside requests Inside looping
body: <p>I have the code below which makes a request to return all scheduled msgs before now and if there are some, it updates each document in the "scheduled" collection with a new date (increasing its value according to the repeat field), and if this document update is done, it inserts a new document on "msgs" collection.</p>
  
  <p>The problem is I did an overly complicated code and it's inserting duplicated msgs at the end. I suspect the function "insertScheduledMsgs" is being called again before the async request to update the "scheduled" document inside the looping finishes, so the next time the function is called, the schedule document still have the old date.</p>
  
  <p>How do I do it simpler and fixing this issue?</p>
  
  <p>Below you can see the whole code:</p>
  
  <pre><code>function insertScheduledMsgs() {
  
  request.get( {
      headers: { 'Authorization':'Bearer ' + token },
      url: urlApi + "scheduleBeforeOrAfterNow?msgsPosition=before"
  }, function( error, response, body ){
      if ( !error &amp;&amp; response.statusCode == 200 ) {
  
          results = JSON.parse( body );
  
          for ( let res in results ){
              let bodyMsg = "",
                  bodySchedule = "",
                  log,
                  scheduleDate = (Number( results[res].date ) + ( Number( results[res].repeat ) * 60 * 1000)),
                  date1 = new Date(results[res].date),
                  date2 = new Date();
  
              bodyMsg += 'user=' + results[res].user;
  
              if ( results[res].media ) {
                  bodyMsg += '&amp;media=' + results[res].media;
              }
  
              bodyMsg += '&amp;txt=' + results[res].txt;
              bodyMsg += '&amp;time=' + results[res].time;
              bodyMsg += '&amp;layout=' + results[res].layout;
              bodyMsg += '&amp;boxes=' + results[res].boxes;
  
              bodySchedule += '_id=' + results[res]._id;
              bodySchedule += '&amp;date=' + scheduleDate;
  
              if( date1.setSeconds(0,0) &lt;=  date2.setSeconds(0,0) ) {
  
                  request.put( {
                      headers: { 'content-type' : 'application/x-www-form-urlencoded', 'Authorization':'Bearer ' + token },
                      url: urlApi + "schedule",
                      body: bodySchedule
                  }, function( errorSchedule, responseSchedule, body ){
  
                      logs({
                          event: "date updated in scheduled msg",
                          date: new Date().getTime()
                      }, token);
  
                      if( date1.setSeconds(0,0) ===  date2.setSeconds(0,0) ) {
  
                          request.post( {
                              headers: { 'content-type' : 'application/x-www-form-urlencoded', 'Authorization':'Bearer ' + token },
                              url: urlApi + "msgs",
                              body: bodyMsg
                          }, function( errorMsg, responseMsg, body ){
  
                              logs({
                                  event: "scheduled msg sent to monitor / tv",
                                  date: new Date().getTime()
                              }, token);
  
                              if ( (Number(res) + 1) === results.length ) {
  
                                  insertScheduledMsgs();
                              }
                          } );
                      } else {
  
                          if ( (Number(res) + 1) === results.length ) {
  
                              insertScheduledMsgs();
                          }
                      }                   
                  } );
              } else {
  
                  if ( (Number(res) + 1) === results.length ) {
  
                      insertScheduledMsgs();
                  }
              }
          };
  
      } else {
  
          insertScheduledMsgs();
  
      }
  } );
  </code></pre>
  
  <p>};</p>