节点js +多个嵌套的内部函数与回调

问题描述:

我有嵌套的内部函数与单一函数(abcd)中使用的回调。节点js +多个嵌套的内部函数与回调

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

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 
    }); 
} 

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; 
        }); 
       }); 
      }); 
     }); 
    }); 
}; 

数据对象需要迭代使用JS异步使用initalFunction功能节点对象的所有数组。

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

我不知道我是否明白你想要达到的目标,但我会试一试。

第一个问题是,您不能在内部回调函数中为您的外部函数设置return值(有关此信息的更多信息,请参见https://*.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); 
} 

我希望这有助于。