与nodejs中的array.some()异步等效

时间:2015-09-18 09:28:02

标签: node.js asynchronous module

我需要迭代一个集合,检查每个集合是否作为我的数据库中的一行存在

是否有.some()异步等效项,用于聚合集合中异步真/假检查的结果?

1 个答案:

答案 0 :(得分:1)

您可以使用async模块,特别是此方法async.each https://github.com/caolan/async#each

https://www.npmjs.com/package/async这是npm包

添加了一个小例子(而不是我想要检查数据库的if

var tests = ['a', 'b'];
async.each(tests, function(test, callback) {

  if(test === 'c') {
    return callback('We cannot have a c')
  }

  return 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('C was found');
    } else {
      console.log('All Tests are ok');
    }
});