节点js +多个嵌套的内部函数,带有回调

时间:2016-03-03 10:23:57

标签: javascript node.js asynchronous callback node-async

我使用单个函数(abcd)中的回调嵌套内部函数。

我需要使用异步从外部调用abcd函数并返回响应。

var listFunctions = {
    test1 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "a");
    },
    test2 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "b");
    },
    test3 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "c");
    },
    test4 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "d");
    },
    test5 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "e");
    }
};

function abcd(objectData, clb) {

    listFunctions.test1(objectData, function(err, data1) {
        listFunctions.test1(data1, function(err, data2) {
            listFunctions.test1(data2, function(err, data3) {
                listFunctions.test1(data3, function(err, data4) {
                    listFunctions.test1(data4, function(err, data5) {
                        return clb;
                    });
                });
            });
        });
    });
};

数组中的数据对象

 var objectData = [{"id":1, "name" : "abcd"},{"id":2, "name" : "abc2d"},{"id":3, "name" : "3abcd"},{"id":4, "name" : "4abcd"}];

initalFunction(objectData, function(response) {
    console.log(response);
});

function initalFunction(objectData, result) {

    async.each(objectData, function(dataValues, callback) {
        abcd(dataValues, function(response) {
            console.log(response);
        });

    }, function(err) {
        return result;
        //Need to re
    });
}

需要使用initalFunction函数使用节点js异步迭代所有对象数组。

在上面我添加了我的代码,请建议正确的方法。

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解你想要达到的目标,但我会试一试。

第一个问题是你不能在内部回调函数中return外部函数的值{(有关详细信息,请参阅https://stackoverflow.com/a/6847754/3173842)。

此外,您将返回回调函数而不是结果。

这是我认为你真正想做的事情:

var async = require("async");

var listFunctions = {
    test1: function(objectData, callbackData) {
        objectData.name += ":a";
        callbackData(null, objectData);
    },
    test2: function(objectData, callbackData) {
        objectData.name += ":b";
        objectData.foo = "bar";
        callbackData(null, objectData);
    },
    test3: function(objectData, callbackData) {
        objectData.name += ":c";
        callbackData(null, objectData);
    },
    test4: function(objectData, callbackData) {
        objectData.name += ":d";
        callbackData(null, objectData);
    },
    test5: function(objectData, callbackData) {
        objectData.name += ":e";
        callbackData(null, objectData);
    }
};

function abcd(objectData, cb) {
    listFunctions.test1(objectData, function(err, data1) {
        listFunctions.test2(data1, function(err, data2) {
            listFunctions.test3(data2, function(err, data3) {
                listFunctions.test4(data3, function(err, data4) {
                    listFunctions.test5(data4, function(err, data5) {
                        cb(err, data5);
                    });
                });
            });
        });
    });
}

function initalFunction(objectData, cb) {
    var results = [];

    async.each(objectData, function(dataValues, done) {
        abcd(dataValues, function(err, response) {
            results.push(response);
            done();
        });
    }, function(err) {
        cb(err, results);
    });
}

var objectData = [{id:1,name:"abcd"},{id:2,name:"abc2d"},{id:3,name:"3abcd"},{id:4,name:"4abcd"}];

initalFunction(objectData, function(err, response) {
    if (err) {
        console.log(err.stack);
        return;
    }
    console.log("Result:", response);
});

现在让我们使用async.seqasync.map来简化:

var async = require("async");

var listFunctions = {
    test1: function(objectData, callbackData) {
        objectData.name += ":a";
        callbackData(null, objectData);
    },
    test2: function(objectData, callbackData) {
        objectData.name += ":b";
        objectData.foo = "bar";
        callbackData(null, objectData);
    },
    test3: function(objectData, callbackData) {
        objectData.name += ":c";
        callbackData(null, objectData);
    },
    test4: function(objectData, callbackData) {
        objectData.name += ":d";
        callbackData(null, objectData);
    },
    test5: function(objectData, callbackData) {
        objectData.name += ":e";
        callbackData(null, objectData);
    }
};

var abcd=async.seq.apply(null, [
    listFunctions.test1,
    listFunctions.test2,
    listFunctions.test3,
    listFunctions.test4,
    listFunctions.test5
]);

function initialFunction(objectData, cb) {
    async.map(objectData, abcd, cb);
}

var objectData = [{id:1,name:"abcd"},{id:2,name:"abc2d"},{id:3,name:"3abcd"},{id:4,name:"4abcd"}];

initialFunction(objectData, function(err, response) {
    if (err) {
        console.log(err.stack);
        return;
    }
    console.log("Result:", response);
});

如果您可以将listFunctions从对象更改为数组:

var async = require("async");

var listFunctions = [
    function test1(objectData, callbackData) {
        objectData.name += ":a";
        callbackData(null, objectData);
    },
    function test2(objectData, callbackData) {
        objectData.name += ":b";
        objectData.foo = "bar";
        callbackData(null, objectData);
    },
    function test3(objectData, callbackData) {
        objectData.name += ":c";
        callbackData(null, objectData);
    },
    function test4(objectData, callbackData) {
        objectData.name += ":d";
        callbackData(null, objectData);
    },
    function test5(objectData, callbackData) {
        objectData.name += ":e";
        callbackData(null, objectData);
    }
];

function initialFunction(objectData, cb) {
    async.map(objectData, async.seq.apply(null, listFunctions), cb);
}

var objectData = [{id:1,name:"abcd"},{id:2,name:"abc2d"},{id:3,name:"3abcd"},{id:4,name:"4abcd"}];

initialFunction(objectData, function(err, response) {
    if (err) {
        return console.error(err.stack);
    }
    console.log("Result:", response);
});

如果没有,但仍需要处理动态listFunctions

function initialFunction(objectData, cb) {
    var list = Object.keys(listFunctions).map(function(name) {
        return listFunctions[name];
    });
    async.map(objectData, async.seq.apply(null, list), cb);
}

我希望这会有所帮助。