async.each 与 async.every 之间的区别?
Difference between async.each vs async.every?
async.each 与 async.every 在 async.js 中的区别?
看起来两者是相同的,除了 async.every returns 结果。
纠正我我错了。
每个(arr,迭代器,[回调])
将函数迭代器并行应用于 arr 中的每个项目。使用列表中的项目调用迭代器,并在完成时回调。如果迭代器将错误传递给它的回调,则立即调用主回调(针对每个函数)并返回错误。
请注意,由于此函数将迭代器并行应用于每个项目,因此无法保证迭代器函数将按顺序完成。
参数
arr - 要迭代的数组。
iterator(item, callback) - 应用于 arr 中每个项目的函数。迭代器传递了一个回调(err),一旦完成就必须调用它。如果没有发生错误,回调应该是 运行 没有参数或带有显式 null 参数。数组索引未传递给迭代器。如果需要索引,请使用 forEachOf。
callback(err) - 可选 当所有迭代器函数完成或发生错误时调用的回调。
例子
// assuming openFiles is an array of file names and saveFile is a function
// to save the modified contents of that file:
async.each(openFiles, saveFile, function(err){
// if any of the saves produced an error, err would equal that error
});
// assuming openFiles is an array of file names
async.each(openFiles, function(file, callback) {
// Perform operation on file here.
console.log('Processing file ' + file);
if( file.length > 32 ) {
console.log('This file name is too long');
callback('File name too long');
} else {
// Do work to process file here
console.log('File processed');
callback();
}
}, function(err){
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
每个(arr,迭代器,[回调])
别名:全部
Returns 如果 arr 中的每个元素都满足异步测试则为真。每个迭代器调用的回调只接受一个 true 或 false 参数;它首先不接受错误参数!这符合节点库处理真值测试的方式,例如 fs.exists.
参数
arr - 要迭代的数组。
iterator(item, callback) - 并行应用于数组中每个项目的真值测试。迭代器被传递一个回调(truthValue),一旦完成,它必须用布尔参数调用。
callback(result) - 可选 任何迭代器 returns false 或所有迭代器函数完成后立即调用的回调。根据异步测试的值,结果将为真或假。
注意:回调不会将错误作为第一个参数。
示例
async.every(['file1','file2','file3'], fs.exists, function(result){
// if result is true then every file exists
});
每个异步
.each(coll, iteratee, callback)
它更像是排列每个方法。在每个 Iterable ( coll ) 元素上,函数 iteratee
将被执行。这将并行进行。所以以网站为例
async.each(openFiles, saveFile, function(err){
// if any of the saves produced an error, err would equal that error
});
这里假设 openFiles
是文件路径数组。所以 saveFile
将在每个人身上被调用。该过程将并行进行。所以执行的顺序是不能保证的。这里将由 saveFile
对 openFiles
数组进行一些操作。如果任何元素导致 saveFile 中的错误,该函数将调用错误的邮件回调并停止该过程。
异步每个
.every(coll, iteratee, callback)
这似乎是相同的方法。因为它也在 coll
元素上执行 iteratee
方法。但这里的关键是,它会 return 或者 true
或者 false
。它更像是一个过滤器,但唯一的区别是它 returns false
如果 coll
中的任何元素在 iteratee 方法中失败。不要将此处与错误混淆。如果在执行过程中发生一些不确定的行为,就会导致错误。所以方法中的 callback
将 return callback(err, result)
。结果是真还是假取决于 coll
是否通过迭代测试。
例如检查数组是否有偶数;
async.every([4,2,8,16,19,20,44], function(number, callback) {
if(number%2 == 0){
callback(null, true);
}else{
callback(null, false);
}
}, function(err, result) {
// if result is true when all numbers are even else false
});
所以它更像是在可迭代实体中测试一组值。如果他们通过了给定的测试。另一个例子可能是检查给定数字是否为素数。
async.each 与 async.every 在 async.js 中的区别? 看起来两者是相同的,除了 async.every returns 结果。 纠正我我错了。
每个(arr,迭代器,[回调])
将函数迭代器并行应用于 arr 中的每个项目。使用列表中的项目调用迭代器,并在完成时回调。如果迭代器将错误传递给它的回调,则立即调用主回调(针对每个函数)并返回错误。
请注意,由于此函数将迭代器并行应用于每个项目,因此无法保证迭代器函数将按顺序完成。
参数
arr - 要迭代的数组。 iterator(item, callback) - 应用于 arr 中每个项目的函数。迭代器传递了一个回调(err),一旦完成就必须调用它。如果没有发生错误,回调应该是 运行 没有参数或带有显式 null 参数。数组索引未传递给迭代器。如果需要索引,请使用 forEachOf。 callback(err) - 可选 当所有迭代器函数完成或发生错误时调用的回调。 例子
// assuming openFiles is an array of file names and saveFile is a function
// to save the modified contents of that file:
async.each(openFiles, saveFile, function(err){
// if any of the saves produced an error, err would equal that error
});
// assuming openFiles is an array of file names
async.each(openFiles, function(file, callback) {
// Perform operation on file here.
console.log('Processing file ' + file);
if( file.length > 32 ) {
console.log('This file name is too long');
callback('File name too long');
} else {
// Do work to process file here
console.log('File processed');
callback();
}
}, function(err){
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
每个(arr,迭代器,[回调])
别名:全部
Returns 如果 arr 中的每个元素都满足异步测试则为真。每个迭代器调用的回调只接受一个 true 或 false 参数;它首先不接受错误参数!这符合节点库处理真值测试的方式,例如 fs.exists.
参数
arr - 要迭代的数组。 iterator(item, callback) - 并行应用于数组中每个项目的真值测试。迭代器被传递一个回调(truthValue),一旦完成,它必须用布尔参数调用。 callback(result) - 可选 任何迭代器 returns false 或所有迭代器函数完成后立即调用的回调。根据异步测试的值,结果将为真或假。 注意:回调不会将错误作为第一个参数。
示例
async.every(['file1','file2','file3'], fs.exists, function(result){
// if result is true then every file exists
});
每个异步
.each(coll, iteratee, callback)
它更像是排列每个方法。在每个 Iterable ( coll ) 元素上,函数 iteratee
将被执行。这将并行进行。所以以网站为例
async.each(openFiles, saveFile, function(err){
// if any of the saves produced an error, err would equal that error
});
这里假设 openFiles
是文件路径数组。所以 saveFile
将在每个人身上被调用。该过程将并行进行。所以执行的顺序是不能保证的。这里将由 saveFile
对 openFiles
数组进行一些操作。如果任何元素导致 saveFile 中的错误,该函数将调用错误的邮件回调并停止该过程。
异步每个
.every(coll, iteratee, callback)
这似乎是相同的方法。因为它也在 coll
元素上执行 iteratee
方法。但这里的关键是,它会 return 或者 true
或者 false
。它更像是一个过滤器,但唯一的区别是它 returns false
如果 coll
中的任何元素在 iteratee 方法中失败。不要将此处与错误混淆。如果在执行过程中发生一些不确定的行为,就会导致错误。所以方法中的 callback
将 return callback(err, result)
。结果是真还是假取决于 coll
是否通过迭代测试。
例如检查数组是否有偶数;
async.every([4,2,8,16,19,20,44], function(number, callback) {
if(number%2 == 0){
callback(null, true);
}else{
callback(null, false);
}
}, function(err, result) {
// if result is true when all numbers are even else false
});
所以它更像是在可迭代实体中测试一组值。如果他们通过了给定的测试。另一个例子可能是检查给定数字是否为素数。