在node.js中使用异步模块嵌套For循环
我正在尝试使用在node.js中使用异步模块嵌套For循环,node.js,asynchronous,async.js,Node.js,Asynchronous,Async.js,我正在尝试使用async模块执行嵌套for循环,以便处理dream回调函数的异步。简单地说,我所做的就是在同一个节点实例中运行几个噩梦对象来浏览网站上的不同链接 根据我读到的内容,我必须调用next()函数,以便通知asyncOfSeries循环转到下一个索引。只有一个for循环就可以正常工作。当我嵌套了asyncOfSeries时,内部next()函数不是在内部循环上执行,而是在外部循环上执行 请参阅代码片段,以便更好地理解: var Nightmare = require('nightma
async
模块执行嵌套for循环,以便处理dream
回调函数的异步。简单地说,我所做的就是在同一个节点
实例中运行几个噩梦
对象来浏览网站上的不同链接
根据我读到的内容,我必须调用next()
函数,以便通知asyncOfSeries
循环转到下一个索引。只有一个for循环就可以正常工作。当我嵌套了asyncOfSeries
时,内部next()
函数不是在内部循环上执行,而是在外部循环上执行
请参阅代码片段,以便更好地理解:
var Nightmare = require('nightmare');
var nightmare = Nightmare({show:true})
var complexObject = {
19:["11","12","13","14","15"],
21:["16"],
22:["17"],
23:["18","19"]
};
//looping on each object property
async.eachOfSeries(complexObject, function(item, keyDo, next){
//here im looping on each key of the object, which are arrays
async.eachOfSeries(item, function(indexs, key, nexts){
nightmare
.//do something
.then(function(body){
nightmare
.//do something
.then(function(body2){
nightmare
.//do something
.then(function(body3){
//Here I call next() and expecting to call the next index of the inner loop
//but is calling the next index of the outer loop and the inner for is just
// executing one time.
next();
});
});
});
});
});
我试图在内部循环之后调用另一个
next()
,但抛出了一个错误。有人知道为什么内部循环只运行一次吗?您需要管理两个回调。内部回调函数“nexts”和外部回调函数“next”。您正在从内部循环调用外部回调
试试这个:
var Nightmare = require('nightmare');
var nightmare = Nightmare({show:true})
var complexObject = {
19:["11","12","13","14","15"],
21:["16"],
22:["17"],
23:["18","19"]
};
//looping on each object property
async.eachOfSeries(complexObject, function(item, keyDo, next){
//here im looping on each key of the object, which are arrays
async.eachOfSeries(item, function(indexs, key, nexts){
nightmare
.//do something
.then(function(body){
nightmare
.//do something
.then(function(body2){
nightmare
.//do something
.then(function(body3){
//Here I call next() and expecting to call the next index of the inner loop
//but is calling the next index of the outer loop and the inner for is just
// executing one time.
nexts();
});
});
});
});
next();
});
您需要管理两个回调。内部回调函数“nexts”和外部回调函数“next”。您正在从内部循环调用外部回调 试试这个:
var Nightmare = require('nightmare');
var nightmare = Nightmare({show:true})
var complexObject = {
19:["11","12","13","14","15"],
21:["16"],
22:["17"],
23:["18","19"]
};
//looping on each object property
async.eachOfSeries(complexObject, function(item, keyDo, next){
//here im looping on each key of the object, which are arrays
async.eachOfSeries(item, function(indexs, key, nexts){
nightmare
.//do something
.then(function(body){
nightmare
.//do something
.then(function(body2){
nightmare
.//do something
.then(function(body3){
//Here I call next() and expecting to call the next index of the inner loop
//but is calling the next index of the outer loop and the inner for is just
// executing one time.
nexts();
});
});
});
});
next();
});
在哪里声明变量
噩梦?我已经编辑了代码段@stdob——因为您使用的是承诺,所以可能不应该使用async.js。它们组合不好(而且你根本不处理错误)。你的输入错误是:nexts
,而不是next
。您必须在内部回调中调用内部的next
,并且必须将外部的next
作为回调传递给eachOfSeries
,在那里您声明了一个变量dream
?我已经编辑了代码段@stdob——因为您使用的是承诺,所以可能不应该使用async.js。它们组合不好(而且你根本不处理错误)。你的输入错误是:nexts
,而不是next
。您必须在内部回调中调用内部next
,并且必须将外部next
作为回调传递给eachOfSeries
绝对合法,我实际上在这里粘贴了一个错误代码,但在我的代码中,我有两个具有相同名称next()的回调。我想,既然它们在引起问题的同一范围内,我就试试这个。谢谢绝对合法,我实际上在这里粘贴代码时有一个拼写错误,但在我的代码中,我有两个同名的回调next()。我想,既然它们在引起问题的同一范围内,我就试试这个。谢谢