标签: node-async

为什么async.map函数与本机fs.stat函数一起使用?

async.map(['file1','file2','file3'], fs.stat, function(err, results){
    // results is now an array of stats for each file
});
Run Code Online (Sandbox Code Playgroud)

根据文档,第二个参数是:

iterator(item,callback) - 一个应用于数组中每个项的函数.

精细.

迭代器传递一个回调(错误,转换),一旦完成错误(可以为null)和转换项,就必须调用它.

我认为这fs.stat不符合这一点,我会说这不应该奏效.

它应该是这样的:

async.map(['file1','file2','file3'],
    function (file, complete) {
        fs.stat(file, function (err, stat) {
            complete(err, stat)
        });
    }, function(err, results){
        // results is now an array of stats for each file
    }
);
Run Code Online (Sandbox Code Playgroud)

asynchronous node.js node-async

2
推荐指数
1
解决办法
2167
查看次数

为什么在快速路由中调用next()是可选的?

在Nodejs/Express的许多示例中,我看到在成功的情况下调用next()是可选的.

exports.postLogin = (req, res, next) => {
  passport.authenticate('local', (err, user, info) => {
    if (err) {
        return next(err);
    }
    req.logIn(user, (err) => {
      if (err) { return next(err); }
      req.flash('success', { msg: 'Success! You are logged in.' });
      res.redirect(req.session.returnTo || '/');
    });
  })(req, res, next);
};
Run Code Online (Sandbox Code Playgroud)

此外,很容易跳过nextargs中的回调:

exports.postLogin = (req, res) => {
  res.render('some-template', locals);
}
Run Code Online (Sandbox Code Playgroud)

如果我将它与asynclib或典型的Javascript异步模型进行比较,则缺少的回调将不会提供数据或停止进程.

这是什么express做时要小心控制流的next不叫?

callback node.js express node-async

2
推荐指数
2
解决办法
1101
查看次数

Node.js - 异步,而循环不迭代

基于上一个问题Illegal break语句(Node.js)的建议,我实现了async.whilst(),但它不是多次迭代.

我试图找到一个唯一的ID,通过增加ID末尾的数字,并查询Mongo以查看该ID是否存在.如果它不存在,则找到唯一ID.它只循环一次,而不是找到唯一的.怎么了?

代码:

 var uniqueNumber = 1;
 var newUnique;

 async.whilst(
    function () { 

       var uniqueNum_string = uniqueNumber.toString(); 
       newUnique = data.id + uniqueNum_string;

       db.collection('landmarks').findOne({'id':newUnique}, function(err, data){

           if (data){
              console.log('entry found!');
              return;
           }

           else {
              console.log('entry not found!');

           }
        });

  },
  function (callback) {

     uniqueNumber++;

   },
   function (err) {

      saveLandmark(newUnique);
   }
);
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous node.js node-async

1
推荐指数
1
解决办法
2823
查看次数

async.apply在async.waterfall中

我有以下代码片段

async.waterfall([
  // Read directory
  async.apply(fs.readdir, '../testdata'),
  // Load data from each file
  function(files, callback) {
    async.each(files, loadDataFromFile, callback);
  }
], function(err) {
  if (err) {
    api.logger.error('Error while inserting test data', err);
  }
  next();
});
Run Code Online (Sandbox Code Playgroud)

有没有办法可以替换这件作品:

function(files, callback) {
  async.each(files, loadDataFromFile, callback);
}
Run Code Online (Sandbox Code Playgroud)

只是一个功能?就像我上面所做的那样,使用async.apply()我替换了这个:

function(callback) {
  fs.readdir('../testdata', callback);
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以创建自己的辅助函数来做到这一点,或者我可以这样离开它,但我想知道是否有办法只使用像.bind()或等函数来做到这一点.apply().

我想过使用.bind().apply()但这将导致function(loadDataFromFile, files, callback)这是不行的.

javascript node.js node-async

1
推荐指数
1
解决办法
3816
查看次数

选择适当的异步方法来进行最大请求数/秒的批处理

我需要对某些外部 API 执行循环调用,但有一定的延迟,以防止“超出用户速率限制”限制。

Google 地图地理编码 API 对“请求/秒”敏感,允许 10 个请求/秒。我应该对数百个联系人进行地理编码,而这样的延迟是必需的。因此,我需要 10 个异步地理编码函数,每个函数的后延迟为 1 秒。因此,我收集数组中的所有联系人,然后以异步方式循环遍历数组。

一般来说,我需要有 N 个并发线程,每个线程结束时有 D 毫秒的延迟。整个循环迭代用户实体数组。像往常一样,每个线程处理单个实体。

我想有这样的代码:

const N = 10;   # threads count
const D = 1000; # delay after each execution

var processUser = function(user, callback){ 
  someBusinessLogicProc(user, function(err) {
    setTimeout(function() {
      return callback(err);
    }, D);
  });      
 }

 var async = require('async') ;
 var people = new Array(900);

 async.batchMethod(people, processUser, N, finalCallback);
Run Code Online (Sandbox Code Playgroud)

在这个伪代码中batchMethod是我要求的方法。

node.js node-async async.js

1
推荐指数
1
解决办法
4375
查看次数