我有一个关于原生数组的问题。JavaScript的每一个实现都是异步的吗? 例如,如果我调用:
[many many elements].forEach(function () {lots of work to do})
这将是非阻塞的吗?
我有一个关于原生数组的问题。JavaScript的每一个实现都是异步的吗? 例如,如果我调用:
[many many elements].forEach(function () {lots of work to do})
这将是非阻塞的吗?
当前回答
如果您需要一个异步友好的Array版本。对于ach和类似的,它们在Node.js 'async'模块中可用:http://github.com/caolan/async…作为奖励,这个模块也可以在浏览器中工作。
async.each(openFiles, saveFile, function(err){
// if any of the saves produced an error, err would equal that error
});
其他回答
这是一个不需要第三方库就可以使用的简短异步函数
Array.prototype.each = function (iterator, callback) {
var iterate = function () {
pointer++;
if (pointer >= this.length) {
callback();
return;
}
iterator.call(iterator, this[pointer], iterate, pointer);
}.bind(this),
pointer = -1;
iterate(this);
};
如果您需要一个异步友好的Array版本。对于ach和类似的,它们在Node.js 'async'模块中可用:http://github.com/caolan/async…作为奖励,这个模块也可以在浏览器中工作。
async.each(openFiles, saveFile, function(err){
// if any of the saves produced an error, err would equal that error
});
在npm中有一个包,可以为每个循环提供简单的异步。
var forEachAsync = require('futures').forEachAsync;
// waits for one request to finish before beginning the next
forEachAsync(['dogs', 'cats', 'octocats'], function (next, element, index, array) {
getPics(element, next);
// then after all of the elements have been handled
// the final callback fires to let you know it's all done
}).then(function () {
console.log('All requests have finished');
});
这也是allasync的另一个变体
在Node中执行非常繁重的计算有一个常见的模式,可能适用于你…
Node是单线程的(作为一个深思熟虑的设计选择,请参阅什么是Node.js?);这意味着它只能利用一个核心。现代的盒子有8个、16个甚至更多的内核,所以这可能会使90%以上的机器闲置。REST服务的常见模式是为每个核心启动一个节点进程,并将这些进程放在本地负载均衡器(如http://nginx.org/)后面。
Forking a child - For what you are trying to do, there is another common pattern, forking off a child process to do the heavy lifting. The upside is that the child process can do heavy computation in the background while your parent process is responsive to other events. The catch is that you can't / shouldn't share memory with this child process (not without a LOT of contortions and some native code); you have to pass messages. This will work beautifully if the size of your input and output data is small compared to the computation that must be performed. You can even fire up a child node.js process and use the same code you were using previously.
例如:
var child_process = require('child_process'); function run_in_child(array, cb) { var process = child_process.exec('node libfn.js', function(err, stdout, stderr) { var output = JSON.parse(stdout); cb(err, output); }); process.stdin.write(JSON.stringify(array), 'utf8'); process.stdin.end(); }
数组中。forEach是用来计算东西而不是等待的,并且在事件循环中使计算异步是没有任何收获的(如果你需要多核计算,web工作者会添加多处理)。如果希望等待多个任务结束,可以使用计数器,可以将计数器包装在信号量类中。