I have this piece of code that runs on a large array:
for (i = 0; i < cars.length; i++) {
cars[i] = some_function(cars[i]) ;
}
and I should run this code in intervals without blocking the event loop. What is a proper way to do it?
For the part not blocking the event loop you have two options, maybe even more:
some_function with process.nextTick to give control back to the event loop between chunksIf chunking is possible you have to figure out as there are no details of your function.
For the part of intervals you should wrap the code inside a setInterval.
There is an excellent asynchronous-handler library called Bluebird that's emerging as the main tool for NodeJS async coordination.
Bluebird has a really useful method pair designed to work with arrays: map and mapSeries. It's simple to use, lets you iterate across arrays without blocking the event loop, and makes the code very readable:
var Promise = require('bluebird'),
cars = ['a', 'b', 'c'];
// Iterate across cars without blocking the event loop. Each
// callback will be made in PARALLEL:
// @see http://bluebirdjs.com/docs/api/promise.map.html
Promise.map(cars, function(car) {
console.log("I'm logging car ' + car + ' but could be any complex code...');
});
// Iterate across cars without blocking the event loop. Each
// callback will be made in SERIES. An item will be fully processed
// before moving on to the next:
// @see http://bluebirdjs.com/docs/api/promise.mapseries.html
Promise.mapSeries(cars, function(car) {
console.log("I'm logging car ' + car + ' but could be any complex code...');
});
setInterval?setTimeoutyou will get expected result. Please look at this answer