gpt4 book ai didi

javascript - Node.JS 的 Async.js 遇到问题

转载 作者:太空宇宙 更新时间:2023-11-04 02:20:22 24 4
gpt4 key购买 nike

我试图对抗回调 hell ,但我的代码遇到了问题。

有人知道如何将 async.js 实现为如下结构:

for(var i=0;i<array1.length;i++){
//do something
for(var j=0;j<array2.length;j++){
//do something
for(var k=0;k<array3.length;k++){
//do something
bd.findSomethig(array3[k].id, function(err, result){

});

bd.findSomethig(array3[k].ref, function(err, result){

});

}
}
}

更新:

我将在这里放置我需要实现的真实结构

     var array1 = array;
var rsp=[];
for(var i=0;i<array1.length;i++){
var object1 = new Object();

object1.a="xx";
object1.b="xx";
object1.c="xx";
object1.d="xx";

var array2=array1[i].array2;

var array2Result=[];
for(var j=0;j<array2.length;j++){

var object2 = new Object();

object2.a="xx";
object2.b="xx";
object2.c="xx";
object2.d="xx";

var array3=array1[i].array2[j].array3;
var array3Result=[];

for(var k=0;k<array3.length;k++){

var object3 = new Object();
var array4=array1[i].array2[j].array3[l].array4;
var array3Result4=[];

for(var l=0;l<array4.length;l++){

var object4 = new Object();
var array5=array1[i].array2[j].array3[k].array4[l].array5;

for (var m=0;m<array5.length;m++){
if(someConditional){

object4.a="xx";
object4.b="xx";
object4.c="xx";
object4.d="xx";

//Here i need to call a BD function to find some rows i need to build the JSON response
bd.findSomethig(array5[m].id, function(err, result){
object4.e=result;
});

bd.findSomethig(array5[m].ref, function(err, result){
object4.f=result;
});

array3Result4.push(object4);
}

}
object3.a=segments;
array3Result.push(object3);
}
object2.e=array3Result;
array2Result.push(object2)
}
object1.e=array2Result;
rsp.push(object1);
}

最佳答案

通常,您的代码将如下所示:

async.map(array1, function(item, next){
var modifiedItem = modify(item);
next(null, modifiedItem);
}, function(err, results){
// results is an array of modifiedItem's here
});

为了获得良好的可读性,您不能按原样编写。
相反,您定义了一些函数,这些函数修改项目并调用 next 回调。
该函数称为迭代器

例如:

function doubleIterator(item, next) {
next(null, 2*item);
}

async.map([1,2,3], doubleIterator, function (err, results) {
// we have results = [2, 4, 6] here
});

此外,我们可以根据需要生成迭代器:

function getFieldIterator(field) {
return function (item, next) {
next(null, item[field]);
}
}
async.map([{id: 1}, {id: 7}], getFieldIterator('id'), function(err, results){
// results = [1, 7]
});

为了应用多维结构,我们可以从迭代器调用async方法:

   function getMapIt(iterator) {
return function(item, next) {
async.map(item, iterator, next);
};
}


async.map([[1, 2], [3, 4]], getMapIt(doubleIterator), function(err, results){
// getMapIt returns iterator which receive two arrays
// it run async.map on each of them
// and apply doubleIterator in each
// so, we have results = [[2, 4], [6, 8]] here
})
<小时/>

更新了问题。这里我们只是构建迭代器然后运行:)

async.map(array1, mapIterator(mapIterator(searchIterator)), callback);
// assume you have just 3-dimensional array
function mapIterator(iterator) {
return function(item, next) {
async.map(item, iterator, next);
}
}

//with structure in your question:
function mapField(field, iterator) {
return function(item, next) {
async.map(item[field], iterator, next);
}
}
// and use it as
// mapField('array2', mapField('array3', searchIterator))

function searchIterator(item, next) {
async.parallel({
id: search(item.id),
ref: search(item.ref)
}, next);
}
function search(id) {
return function(next){
bd.findSomethig(id, next);
}
}
function callback(err, results) {
if (err) return console.error(err);
console.log('Batch of results: ',results);
// It should be represented same of array1 structure:
/*
[
[
[{id: dbRes, ref: dbRes}, {id: dbRes, ref: dbRes}],
[{id: dbRes, ref: dbRes}, {id: dbRes, ref: dbRes}],
[{id: dbRes, ref: dbRes}, {id: dbRes, ref: dbRes}]
],
[
[{id: dbRes, ref: dbRes}, {id: dbRes, ref: dbRes}],
[{id: dbRes, ref: dbRes}, {id: dbRes, ref: dbRes}]
]

]
*/

}

可能需要在这里发挥限制......
但构建这个的概念现在应该很清楚了。

关于javascript - Node.JS 的 Async.js 遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33746107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com