我在单个函数(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) 我使用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)
如果在上面,我可以在其他内部使用"继续"等效吗?这在技术上不属于循环,所以继续不起作用.
我试图通过使用减少异步调用(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) 我使用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
这是我的代码示例:
async.each(items, cb, function(item) {
saveItem.then(function(doc) {
cb();
});
}, function() {
});
Run Code Online (Sandbox Code Playgroud)
saveItem是一个承诺.当我运行这个时,我总是得到cb is undefined,我猜then()是没有访问权限.任何想法如何解决这个问题?
我试图找出最快的方法而不影响性能,同时使用Node.js同步或异步移动100.000+文件
我有3个不同的循环由同步和异步测试forEach async.each和for.循环迭代不会对时序结果产生太大影响.影响性能和速度的最重要的逻辑是同步或异步移动文件.
测试用例显示,同步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) 我正在使用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) 我想在我的服务器节点上启动异步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]
所以说水果是一个包含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秒,然后打印整个列表.:\有谁知道如何实现我的预期行为?
我正在尝试在一系列项目上运行 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) node-async ×10
node.js ×10
asynchronous ×7
javascript ×6
async.js ×2
callback ×1
http ×1
loops ×1
performance ×1
progress-bar ×1
q ×1