Node Async - Callback was already called - using 2 for loops and an if statement

南楼画角 提交于 2020-01-06 07:27:18

问题


Could someone please explain why I keep seeing the Error: Callback was already called.

I feel I have covered all cases - so why would be 'already called':

function addVertices(outercallback) {

    async.forEachLimit(fullData, 25, function (data, innercallback) {

        myJson.matches.forEach(function (oMatches) {
            if (data.$.id == oMatches.SourceId) {

                oMatches.Ids.forEach(function (oId) {

                    client.execute("g.addV('test').property('id', \"" + oId + "\")", {}, (err, results) => {
                        if (err) {
                            return innercallback(console.error(err));
                        }

                        innercallback(null)
                    });

                })

            } 

        })

    }, outercallback);

}

Infact I also tried replacing that second forEach with async.ForEach as in the following, however is this the correct way to go?:

function addVertices(outercallback) {

    async.forEachLimit(fullData, 25, function (data, innercallback) {

        async.forEach(myJson, function (oMatches, innercallback2) {
            if (data.$.id == oMatches.SourceId) {

                oMatches.Ids.forEach(function (oId) {

                    client.execute("g.addV('test').property('id', \"" + oId + "\")", {}, (err, results) => {
                        if (err) {
                            return innercallback2(console.error(err));
                        }

                        innercallback2(null)
                    });

                })

            } 
            innercallback(null)

        })

    }, outercallback);

}

回答1:


That is not the problem of if.

But because in each iteration of async.forEachLimit, the innercallback is called in the myJson.matches.forEach(...) which may have been called multiple times since myJson.matches may be more than 1 item.

Same problem in your 2nd code block.
In the inner async.forEach, the innercallback2 is called in the oMatches.Ids.forEach which also may be called multiple times.

Therefore, Error: Callback was already called is argued when your second call (or any later calls) to innercallback occurs.

I've updated the solution with Promise#all to ensure the call of innercallback in your original post. See the Updated (2018.05.14) part and the code block.



来源:https://stackoverflow.com/questions/50284161/node-async-callback-was-already-called-using-2-for-loops-and-an-if-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!