async.whilst中的callback和err有什么用?
What is the callback and err in async.whilst used for?
我正在尝试使用 async.whilst 重新生成一个介于 0 和数组长度之间的随机数,直到该索引上的元素长度大于指定长度。我想为此使用 async.whilst,但语法对我来说并不完全清楚。我考虑过执行以下操作:
var selectParagraph = function(paragraphs, callback){
var index = Math.floor(Math.random() * paragraphs.length
async.whilst(
function(){
return paragraphs[index].length < minParagraphLength;
},
function(cb) {
index = Math.floor(Math.random() * paragraphs.length);
},
function(err) {
console.log(paragraphs[index]);
callback(err, paragraphs[index]);
}
}
但是,这不起作用。我想这是因为我没有在任何地方将 cb 用于第二个功能,但我不完全知道我应该如何使用它。更改索引后我只调用 cb() 吗?变量err到底包含什么?
I suppose it is because I didn't use the callback for the second function anywhere
是的,没错。 async.js
希望您在完成后回调,如果没有完成,它将不会继续进行下一次迭代。
but I don't exactly know how I should use it
你根本不应该使用它,因为你没有做任何异步的事情。使用标准 do while
循环:
do {
var index = Math.floor(Math.random() * paragraphs.length);
} while (paragraphs[index].length < minParagraphLength)
console.log(paragraphs[index]);
callback(null, paragraphs[index]); // not sure where you're getting `callback` from
作为 ,您没有做任何异步操作,根本不需要使用 whilst
。但是,我将进一步解决您对 whilst
如何工作的具体困惑。
whilst
的第二个参数是一个可以执行异步操作的函数。 whilst
无法知道函数何时 "done" 完成了它需要做的所有事情。回调参数是一种向 whilst
发出此函数已完全完成其所有任务并且 whilst
可以继续下一次迭代的信号。
假设我们想要每隔一秒制作一系列控制台消息。 (这是一个非常人为的例子,但我想不出一个更容易解释的自然例子。)
var i = 0;
async.whilst(
function(){ return i < 5; },
function(cb) {
setTimeout(function() {
console.log(i++);
cb();
}, 1000);
},
function(err) { console.err("we encountered an error", err); }
);
在这种情况下,我们调用setTimeout
然后whilst
不会执行下一次迭代,直到在setTimeout
决议中调用cb()
。如果 whilst
运行 函数一结束就进行下一次迭代,所有 setTimeout
调用将同时排队,而不是 运行 一个接一个地排队.相反,它会等到函数调用 cb()
.
由于您从未在代码中调用 cb()
,因此 whilst
只是假定该函数已分派了一个耗时很长的异步任务。 whilst
不会 运行 下一次迭代,直到第一个函数调用通过调用 cb()
.
确认它已完成
不带参数调用 cb()
表明该函数已顺利完成其任务。如果函数以错误结束(无法读取文件,无法访问网络资源),那么您可以将该错误作为参数提供给 cb
(例如 cb(new Error("could not reach the server"));
)和错误将提供给 whilst
的第三个函数参数。
我正在尝试使用 async.whilst 重新生成一个介于 0 和数组长度之间的随机数,直到该索引上的元素长度大于指定长度。我想为此使用 async.whilst,但语法对我来说并不完全清楚。我考虑过执行以下操作:
var selectParagraph = function(paragraphs, callback){
var index = Math.floor(Math.random() * paragraphs.length
async.whilst(
function(){
return paragraphs[index].length < minParagraphLength;
},
function(cb) {
index = Math.floor(Math.random() * paragraphs.length);
},
function(err) {
console.log(paragraphs[index]);
callback(err, paragraphs[index]);
}
}
但是,这不起作用。我想这是因为我没有在任何地方将 cb 用于第二个功能,但我不完全知道我应该如何使用它。更改索引后我只调用 cb() 吗?变量err到底包含什么?
I suppose it is because I didn't use the callback for the second function anywhere
是的,没错。 async.js
希望您在完成后回调,如果没有完成,它将不会继续进行下一次迭代。
but I don't exactly know how I should use it
你根本不应该使用它,因为你没有做任何异步的事情。使用标准 do while
循环:
do {
var index = Math.floor(Math.random() * paragraphs.length);
} while (paragraphs[index].length < minParagraphLength)
console.log(paragraphs[index]);
callback(null, paragraphs[index]); // not sure where you're getting `callback` from
作为 whilst
。但是,我将进一步解决您对 whilst
如何工作的具体困惑。
whilst
的第二个参数是一个可以执行异步操作的函数。 whilst
无法知道函数何时 "done" 完成了它需要做的所有事情。回调参数是一种向 whilst
发出此函数已完全完成其所有任务并且 whilst
可以继续下一次迭代的信号。
假设我们想要每隔一秒制作一系列控制台消息。 (这是一个非常人为的例子,但我想不出一个更容易解释的自然例子。)
var i = 0;
async.whilst(
function(){ return i < 5; },
function(cb) {
setTimeout(function() {
console.log(i++);
cb();
}, 1000);
},
function(err) { console.err("we encountered an error", err); }
);
在这种情况下,我们调用setTimeout
然后whilst
不会执行下一次迭代,直到在setTimeout
决议中调用cb()
。如果 whilst
运行 函数一结束就进行下一次迭代,所有 setTimeout
调用将同时排队,而不是 运行 一个接一个地排队.相反,它会等到函数调用 cb()
.
由于您从未在代码中调用 cb()
,因此 whilst
只是假定该函数已分派了一个耗时很长的异步任务。 whilst
不会 运行 下一次迭代,直到第一个函数调用通过调用 cb()
.
不带参数调用 cb()
表明该函数已顺利完成其任务。如果函数以错误结束(无法读取文件,无法访问网络资源),那么您可以将该错误作为参数提供给 cb
(例如 cb(new Error("could not reach the server"));
)和错误将提供给 whilst
的第三个函数参数。