async.forEachSeries,后跟array.forEach

时间:2018-11-29 06:38:10

标签: javascript node.js api asynchronous

说我有一个函数,其中async.forEachSeries以array.forEach()开头。执行的顺序是什么?在array.forEach()完成之后,async.forEachSeries是否执行?

function(){
  array.forEach(){ push into array something}
  async.forEachSeries(array){
    request.get(array.element)
  }

}

我有以下代码并不总是按预期运行,它在大多数时间都运行,但有时在诸如sresult.data.push(...)这样的行上它表示sresult.data是未定义的。我能理解的最可能原因是代码未按顺序运行,因此sresult.data在运行时未定义。有人可以帮忙吗?

 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 => {
                                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 => {
                                                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 => {
                                                        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)
                    })
                })
            }

0 个答案:

没有答案