nodejs async模块:https://github.com/caolan/async提供了2 种类似的方法,async.waterfall和async.series.
他们之间有什么区别?
我正在使用节点async lib - https://github.com/caolan/async#forEach,并希望迭代一个对象并打印出它的索引键.一旦完成,我想执行回调.
这是我到目前为止,但'iterating done'从未见过:
async.forEach(Object.keys(dataObj), function (err, callback){
console.log('*****');
}, function() {
console.log('iterating done');
});
Run Code Online (Sandbox Code Playgroud)
为什么不调用最终函数?
如何打印对象索引键?
我正在使用caolan的async.js库,特别是.each方法.
如何访问迭代器中的索引?
async.each(ary, function(element, callback){
//do stuff here for each element in ary
//how do I get access to the index?
}, function(err) {
//final callback here
})
Run Code Online (Sandbox Code Playgroud) var async = require('async');
async.parallel([
function(cb) {
cb(true);
},
function(cb) {
cb(null, true);
}],
function(error, results) {
}
);
Run Code Online (Sandbox Code Playgroud)
在代码中,如果第一个任务在第二个任务之前运行cb(true),第二个任务是否仍会运行?如果是这样,完成后,主回调仍然会被调用吗?
我正在研究node.js模块的异步,但我对函数async.retry有一些问题.
根据它的github文档,该函数将继续尝试任务,直到它成功或机会用完为止.但是我的任务怎样才能说明成败?
我试过下面的代码:
var async = require('async');
var opts = {
count : -3
};
async.retry(5, function (cb, results) {
++this.count;
console.log(this.count, results);
if (this.count > 0) cb(null, this.count);
else cb();
}.bind(opts), function (err, results) {
console.log(err, results);
});
Run Code Online (Sandbox Code Playgroud)
我希望它能一直运行count === 1,但它总会打印出来:
-2 undefined
undefined undefined
Run Code Online (Sandbox Code Playgroud)
那我怎么能正确使用这个功能呢?
我有一个问题,关于将async.waterfall()中的参数传递给第三个函数而不是第一个函数.例如,如下
async.waterfall([
first,
second,
async.apply(third, obj)
], function(err, result){});
Run Code Online (Sandbox Code Playgroud)
现在可以在名为third的函数中使用"obj"作为参数,并且还可以使用从名为second的函数的回调传递下来的参数
这段代码直接取自以下示例:https://github.com/caolan/async#seriestasks-callback
var async = require("async");
async.series([
function() { console.log("a"); },
function() { console.log("b"); }
], function(err, results){
console.log(err);
console.log(results);
});
Run Code Online (Sandbox Code Playgroud)
但它不起作用.打印"a"后停止.
它是最新版本的异步模块的错误还是我的用法有问题?
我在node.js中编写了一个执行网络操作的模块.我写了一个使用这个模块的小脚本(check下面的变量).它看起来像这样:
check(obj, function (err, results) {
// ...
console.log("Check completed");
});
Run Code Online (Sandbox Code Playgroud)
现在这是有趣的事情.当此代码作为mocha测试的一部分执行时,测试将按预期退出.我看到打印了日志语句,并且进程退出.
当代码作为独立节点脚本执行时,会打印日志语句,但进程只会挂起.
当我尝试调试它并使用--debug-brk和启动程序时node-inspector,它会提前退出!我看到了这个process.on 'exit'.当模块中的一些内部回调尚未被调用时,它退出.所以上面的日志语句也没有打印出来.
我现在卡住了,不知道为什么会这样.有没有人见过类似的行为?
我是后端的Node.Js和JavaScript Web开发的新手.我看到回调内部的回调可能很痛苦,并且有模块可以避免这种情况.其中一个模块是async,https://github.com/caolan/async
我已经阅读了文档,但很难开始并理解如何做到这一点.
例如,我有这个函数"check_aut_user",如何使用async转换此代码?
function check_auth_user(username, password, done) {
var client = new pg.Client("pg://user:pass@127.0.0.1/database");
client.connect(function(err) {
// If not get the connection
if(err) { return console.error('could not connect to postgres', err); }
// Query the user table
client.query('select * from "user" where username = $1 and password = $2', [username, password], function(err, result) {
if(err) { return console.error('error running query', err); }
if (result.rowCount > 0) {
var res = result.rows[0];
console.log(res);
passport.serializeUser(function(res, done) {
//console.log("serializer: " + res); …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行一系列函数,每个函数都将回调传递给下一个.现在它看起来像这样(原谅任何小错误,我在发布时重写它!):
function func1(callback) {
callback(null, "stuff");
}
function func2(input, callback) {
callback(null, "foo" + input);
}
async.waterfall([func1, func2], function(err, result) {
sys.puts(result);
});
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是,我不确定如何优雅地启动此功能,因为它无法接受输入.我最终将把它包装在一个本地函数中,但它仍然让我有点不安.
其次,虽然这有效,但我不知道"错误"的论点是如何发挥作用的.如果我尝试将其插入到参数列表中,它会以各种方式中断.我希望能够单独捕获任何函数中的错误 - 或者这是否需要,因为我在传递的最后一个回调上有错误?