title: async.forEachSeries followed by array.forEach
body: <p>Say I have a function in which async.forEachSeries is preceeded by array.forEach(). What will be the order of execution ? Does async.forEachSeries execute after array.forEach() finishes ?</p>
  
  <pre><code>function(){
    array.forEach(){ push into array something}
    async.forEachSeries(array){
      request.get(array.element)
    }
  
  }
  </code></pre>
  
  <p>I have the following code which does not always run as intended,it runs most of the time but sometimes at lines like sresult.data.push(...) it says that sresult.data is undefined. The most probable reason that I could understand is that the code is not running in sequence and hence, sresult.data is not defined when it runs. Can anyone help with this ?</p>
  
  <pre><code> function (callback) {
                  request.get(config.url.sectionMaster + "?parentSectionCode=" + sectionCode, function (err, result) {
                      // console.log(1)
                      // console.log(result)
                      if (err) { return next(err) }
                      if (result != undefined) {
                          result = JSON.parse(result.body)
                          let firstHeading = result.data
                          if (firstHeading != null) {
                              firstHeading.forEach(element =&gt; {
                                  firstHeadingArray.push({
                                      "entityId": entityId,
                                      "finYear": finYear,
                                      "sectionCodeHead": sectionCode,
                                      "unit": unit,
                                      "sectionCodeDet": element.sectionCode,
                                      "sectionName": element.sectionName,
                                      "asOfDate": "",
                                      "childAmount": "0.00",
                                      "parentAmount": "0.00",
                                      "manualEntry": 0,
                                      "dispOrder": element.dispOrder,
                                      "AppStatus": "",
                                      "Status": "",
                                      "WF_SERIALNUMBER": "",
                                      "WF_ACTION": "",
                                      "leafNode": "",
                                      "data": []
                                  })
                              });
                          } else {
                              return next("No first heading")
                          }
                      } else {
                          return next("Parent section code undefined.")
                      }
                      async.forEachSeries(firstHeadingArray, function (felement, fcallback) {
                          console.log(2)
                          request.get(config.url.sectionMaster + "?parentSectionCode=" + felement.sectionCodeDet, function (err, fresult) {
                              if (err) { return next(err) }
                              if (fresult != undefined) {
                                  fresult = JSON.parse(fresult.body)
                                  sheading = fresult.data
                                  if (sheading != null) {
                                      sheading.forEach(function (element) {
                                          if (felement.data != undefined) {
                                              felement.data.push({
                                                  "entityId": entityId,
                                                  "finYear": finYear,
                                                  "sectionCodeHead": sectionCode,
                                                  "unit": unit,
                                                  "sectionCodeDet": element.sectionCode,
                                                  "sectionName": element.sectionName,
                                                  "asOfDate": "",
                                                  "childAmount": "0.00",
                                                  "parentAmount": "0.00",
                                                  "manualEntry": 0,
                                                  "dispOrder": element.dispOrder,
                                                  "AppStatus": "",
                                                  "Status": "",
                                                  "WF_SERIALNUMBER": "",
                                                  "WF_ACTION": "",
                                                  "leafNode": "",
                                                  "data": []
                                              })
                                          } else {
                                              console.log(felement)
                                              return next({ message: "Error 1" })
                                          }
                                      })
  
                                  } else { }
                              } else { return next({ message: "Parent section code undefined." }) }
                              async.forEachSeries(felement.data, function (selement, scallback) {
                                  console.log(3)
                                  request.get(config.url.sectionMaster + "?parentSectionCode=" + selement.sectionCodeDet, function (err1, sresult) {
                                      if (err1) { return next(err1) }
                                      if (sresult != undefined) {
                                          sresult = JSON.parse(sresult.body)
                                          theading = sresult.data
                                          if (theading != null) {
                                              theading.forEach(telement =&gt; {
                                                  selement.data.push({
                                                      "entityId": entityId,
                                                      "finYear": finYear,
                                                      "sectionCodeHead": sectionCode,
                                                      "unit": unit,
                                                      "sectionCodeDet": telement.sectionCode,
                                                      "sectionName": telement.sectionName,
                                                      "asOfDate": "",
                                                      "childAmount": "0.00",
                                                      "parentAmount": "0.00",
                                                      "manualEntry": 0,
                                                      "dispOrder": telement.dispOrder,
                                                      "AppStatus": "",
                                                      "Status": "",
                                                      "WF_SERIALNUMBER": "",
                                                      "WF_ACTION": "",
                                                      "leafNode": "",
                                                      "data": []
                                                  })
                                              });
                                          } else { }
                                      } else { return next({ message: "Parent section code undefined." }) }
                                      async.forEachSeries(selement.data, function (belement, abc) {
                                          console.log(4)
                                          request.get(config.url.sectionMaster + "?parentSectionCode=" + belement.sectionCodeDet, function (err2, bresult) {
                                              if (err2) { return next(err2) }
                                              if (bresult != undefined) {
                                                  bresult = JSON.parse(bresult.body)
                                                  bheading = bresult.data
                                                  if (bheading != null) {
                                                      bheading.forEach(helement =&gt; {
                                                          belement.data.push({
                                                              "entityId": entityId,
                                                              "finYear": finYear,
                                                              "sectionCodeHead": sectionCode,
                                                              "unit": unit,
                                                              "sectionCodeDet": helement.sectionCode,
                                                              "sectionName": helement.sectionName,
                                                              "asOfDate": "",
                                                              "childAmount": "0.00",
                                                              "parentAmount": "0.00",
                                                              "manualEntry": 0,
                                                              "dispOrder": helement.dispOrder,
                                                              "AppStatus": "",
                                                              "Status": "",
                                                              "WF_SERIALNUMBER": "",
                                                              "WF_ACTION": "",
                                                              "leafNode": "",
                                                              "data": []
                                                          })
                                                      })
                                                  }
                                              }
  
                                              abc()
                                          })
                                      }, function (err) {
                                          scallback()
                                      })
  
                                  })
                              }, function (err) {
                                  fcallback()
                              })
                          })
                      }, function (err) {
                          callback(err, firstHeadingArray)
                      })
                  })
              }
  </code></pre>