标签: node-async

节点js +带有回调的多个嵌套内部函数

我在单个函数(abcd)中使用回调嵌套了内部函数.

我需要使用异步从外部调用abcd函数并返回响应.

var listFunctions = {
    test1 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "a");
    },
    test2 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "b");
    },
    test3 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "c");
    },
    test4 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "d");
    },
    test5 : function(objectData, callbackData) {
        //have logic and retrun data
        callbackData(null, "e");
    }
};

function abcd(objectData, clb) {

    listFunctions.test1(objectData, function(err, data1) { …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous callback node.js node-async

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

使用node.js async forEachSeries时是否有等效的'continue'语句?

我使用node.js异步包,特别是forEachSeries,根据从数组中提取的参数发出一系列http请求.在每个请求的回调中,我有一些if/else语句来响应不同类型的响应.

// This is the callback of a GET request inside of a forEachSeries
function(error, response) {
    if (response.results) {
        // Do something with results
    }
    else if (!response.results) {
        // Would like to use a continue statement here, but
        // this is not inside of a loop
    }
    else {
        // Do something else
    }
}
Run Code Online (Sandbox Code Playgroud)

如果在上面,我可以在其他内部使用"继续"等效吗?这在技术上不属于循环,所以继续不起作用.

javascript asynchronous node.js node-async async.js

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

使用async.waterfall减少嵌套但增加了混乱

我试图通过使用减少异步调用(node + socket.io)的嵌套,async.waterfall我最终不得不在瀑布中追加参数,因为以后需要它们.此代码可能更好地解释:

//原始版本:

 socket event: turn action
  socket.on('turn action', function(gameId, turnAction, clientFn) {
    socket.get('corp', function(err, corp) {
      gameProvider.processTurnAction(gameId, corp.id, turnAction, function(err, msg, game) {
        clientFn(msg, game);
      });
    });
  });
Run Code Online (Sandbox Code Playgroud)

// async.js版本

  async.waterfall([
    function(callback) {
      socket.on('turn action', function(gameId, turnAction, clientFn) {        
        callback(null, gameId, turnAction, clientFn);
      });
    },
    function(gameId, turnAction, clientFn, callback) {
      socket.get('corp', function(err, corp) {
        callback(null, gameId, turnAction, clientFn, corp);
      });
    },
    function(gameId, turnAction, clientFn, corp, callback) {
      gameProvider.processTurnAction(gameId, corp.id, turnAction, function(err, msg, game) {
        clientFn(msg,game);
      }); …
Run Code Online (Sandbox Code Playgroud)

node.js node-async

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

Node.JS async.parallel不会等到所有任务都完成

我使用aync.parallel并行运行两个函数.这些函数请求RSS提要.然后解析RSS提要并将其添加到我的网页.

但由于某种原因async.parallel运行回调方法而不等到两个函数完成

文件说:

任务完成后,结果将作为数组传递给最终回调.

我的代码.

require('async').parallel([ function(callback) {
        fetchRss(res, bbcOpts); // Needs time to request and parse
        callback();
    }, function(callback) {
        // Very fast.
        callback();
    } ], function done(err, results) {
        if (err) {
            throw err;
        }
        res.end("Done!");
    });
Run Code Online (Sandbox Code Playgroud)

事实上我只有"完成!" 在我的网页上.为什么?

我为什么需要打电话res.end()

Node.js的文件说:

必须在每个响应上调用方法response.end().

如果我不打电话,我的网页将被"下载"(我的意思是我的浏览器地址栏中的进度条).

javascript parallel-processing node.js progress-bar node-async

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

nodejs中的async和Q promise

我在nodejs中使用Q库和异步库.

这是我的代码示例:

async.each(items, cb, function(item) {

 saveItem.then(function(doc) {
    cb();
 });

}, function() {

});
Run Code Online (Sandbox Code Playgroud)

saveItem是一个承诺.当我运行这个时,我总是得到cb is undefined,我猜then()是没有访问权限.任何想法如何解决这个问题?

asynchronous node.js node-async q

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

在Node.js同步/异步性能和速度中移动100.000+个文件

我试图找出最快的方法而不影响性能,同时使用Node.js同步或异步移动100.000+文件

我有3个不同的循环由同步和异步测试forEach async.eachfor.循环迭代不会对时序结果产生太大影响.影响性能和速度的最重要的逻辑是同步或异步移动文件.

测试用例显示,同步renameSync()速度较慢,为20%,然后是异步rename().同时异步rename()使用3倍以上的CPU功率,然后同步renameSync().


我是否正确地认为使用同步更明智,renameSync()因为异步中的速度提升rename()不是那么重要且等于20%.虽然异步版本的CPU利用率开销似乎很大 - 300%?

或者我错过了什么?也许在速度/性能方面有更好的解决方法.

时间安排:

eachAsyncRenameAsync()

node rename.js  1.60s user 11.20s system 280% cpu 4.559 total
node rename.js  1.65s user 11.82s system 284% cpu 4.732 total
node rename.js  1.64s user 11.84s system 292% cpu 4.606 total


eachSyncRenameSync()

node rename.js  0.69s user 5.01s system 97% cpu 5.851 total
node rename.js  0.67s user 4.88s system 97% cpu 5.687 total
node rename.js  0.68s …
Run Code Online (Sandbox Code Playgroud)

javascript performance asynchronous node.js node-async

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

读取所有文件后,使用async模块触发回调

我正在使用caolan的'async'模块打开一个文件名数组(在本例中是模板文件名).

根据文档,我正在使用async.forEach(),所以我可以在所有操作完成后触发回调.

一个简单的测试用例是:

var async = require('async')
var fs = require('fs')

file_names = ['one','two','three'] // all these files actually exist

async.forEach(file_names, 
    function(file_name) {
        console.log(file_name)
        fs.readFile(file_name, function(error, data) {
            if ( error) {   
                console.log('oh no file missing')   
                return error
            } else {
                console.log('woo '+file_name+' found')
            }       
        })
    }, function(error) {
        if ( error) {   
            console.log('oh no errors!')
        } else {
            console.log('YAAAAAAY')
        }
    }
)
Run Code Online (Sandbox Code Playgroud)

输出如下:

one
two
three
woo one found
woo two found
woo three found …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous node.js node-async async.js

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

使用nodeJS进行异步http调用

我想在我的服务器节点上启动异步http调用,我看到了async节点模块,我想async.parallel我们可以做到这一点.

记录的示例非常清楚,但我不知道如何管理多个http调用.

我尝试了下面的示例,但它甚至没有启动http调用:

var http = require('http');

var Calls = [];
Calls.push(function(callback) {
    // First call
    http.get('http://127.0.0.1:3002/first' callback);
});

Calls.push(function(callback) {
    // Second call
     http.get('http://127.0.0.1:3002/second' callback);
});

var async = require('async');
async.parallel(Calls, function(err, results) {
    console.log('async callback: '+JSON.stringify(results));
    res.render('view', results);
});
Run Code Online (Sandbox Code Playgroud)

如果我单独启动http请求,我确实有一个结果,但我调用异步回调 async callback: [null,null]

http node.js node-async

4
推荐指数
1
解决办法
3万
查看次数

async.forEach NodeJS中的暂停/超时

所以说水果是一个包含4个项目的数组我所期望的是下面的代码会打印水果,每个水果之间有4秒的延迟.

var fruits = ['blueberries', 'strawberries', 'mango', 'peaches'];
async.forEach(fruits, functions(fruit, next) { 
     setTimeout(function() {
          console.log(fruit);
     }, 4000);
})
Run Code Online (Sandbox Code Playgroud)

实际行为是它等待4秒,然后打印整个列表.:\有谁知道如何实现我的预期行为?

loops asynchronous node.js node-async

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

async.each 里面的 async.waterfall 不起作用?

我正在尝试在一系列项目上运行 async.each。

对于每个项目,我想运行一个 async.waterfall。请参阅下面的代码。

var ids = [1, 2];

async.each(ids, 

    function (id, callback) {

        console.log('running waterfall...');

        async.waterfall([

            function (next) {

                console.log('running waterfall function 1...');

                next();
            },

            function (next) {

                console.log('running waterfall function 2...');

                next();
            }],

            function (err) {

                if (err) {
                    console.error(err);
                }
                else {
                    console.log('waterfall completed successfully!');
                }

                callback();
            });
    }, 

    function (err) {

        if (err) {
            console.error(err);
        }
        else {
            console.log('each completed successfully!');
        }

    });

return;
Run Code Online (Sandbox Code Playgroud)

此代码的输出如下所示:

running waterfall...
running waterfall function 1...
running waterfall...
running waterfall function …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous node.js node-async

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