【问题标题】:Difference between async.each vs async.every?async.each 与 async.every 之间的区别?
【发布时间】:2017-11-30 10:25:27
【问题描述】:

async.js 中的 async.each 与 async.every 之间的区别? 除了 async.every 返回结果之外,两者似乎都是相同的。 纠正我我错了。

【问题讨论】:

标签: javascript node.js express async.js


【解决方案1】:

每个异步

.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 将在每一个上被调用。该过程将是并行的。所以不能保证执行的顺序。这里将由saveFileopenFiles 数组进行一些操作。如果任何元素导致 saveFile 中的错误,该函数将调用带有错误的邮件回调并停止该过程。

每个异步

.every(coll, iteratee, callback)

这似乎是相同的方法。因为它还在coll 元素上执行iteratee 方法。但这里的关键是,它将返回truefalse。它更像是一个过滤器,但唯一的区别是如果 coll 中的任何元素在 iteratee 方法中失败,它会返回 false。不要在这里与错误混淆。如果在执行过程中发生一些不确定的行为,将会导致错误。所以callback in 方法将返回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
});

所以它更像是在可迭代实体中测试一组值。如果他们通过给定的测试。另一个例子是检查给定的数字是否是素数。

【讨论】:

  • 我们可以返回除真假之外的任何东西吗?
  • @RaviTeja 不,该方法的唯一目的是过滤并返回一个布尔值。我用过那个方法,在最终回调中你永远不能得到布尔值以外的值。
【解决方案2】:

每个(arr,迭代器,[回调])

将函数迭代器并行应用于 arr 中的每个项目。使用列表中的项目调用迭代器,并在完成时回调。如果迭代器将错误传递给它的回调,主回调(对于每个函数)会立即调用错误。

请注意,由于此函数将迭代器并行应用于每个项目,因此无法保证迭代器函数将按顺序完成。

参数

arr - 要迭代的数组。 iterator(item, callback) - 应用于 arr 中每个项目的函数。迭代器被传递一个回调(err),一旦完成就必须调用它。如果未发生错误,则应在不带参数或显式空参数的情况下运行回调。数组索引不传递给迭代器。如果需要索引,请使用 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');
    }
});

every(arr, iterator, [callback])

别名:全部

如果 arr 中的每个元素都满足异步测试,则返回 true。每个迭代器调用的回调只接受一个 true 或 false 参数;它首先不接受错误参数!这与节点库处理 fs.exists 等真值测试的方式一致。

参数

arr - 要迭代的数组。 iterator(item, callback) - 并行应用于数组中每个项目的真值测试。迭代器被传递一个回调(truthValue),一旦完成,它必须用一个布尔参数调用。 callback(result) - 可选 任何迭代器返回 false 或所有迭代器函数完成后立即调用的回调。结果将是真或假,具体取决于异步测试的值。 注意:回调不会将错误作为第一个参数。

例子

async.every(['file1','file2','file3'], fs.exists, function(result){
    // if result is true then every file exists
});

【讨论】:

    猜你喜欢
    • 2016-05-17
    • 2015-07-25
    • 2019-04-09
    • 2014-10-30
    • 2021-12-07
    • 2016-02-09
    • 2016-05-14
    • 2016-10-11
    • 2015-06-02
    相关资源
    最近更新 更多