I am trying to understand callbacks properly so that I can iterate arrays without blocking the Server.
Please tell me why it is blocking.. what am I doing wrong? I would much rather run this loop, and then do something when it is finished, but not block the rest of the server.
Here is the code:
//callback test
var myID = 666;
var grid = [
[0,0,0],
[0,0,0],
[0,0,0]
];
function find_empty_slot(id,callback){
console.log("Your ID is" + id);
callback();
}
find_empty_slot(myID,function(){
for(var y = 0, len = grid.length; y < len; y++){
for(var x = 0; x < grid[y].length; x++){
if(grid[y][x] === 0){
console.log("I am a Callback");
}
}
}
});
console.log("This should fire before the Callback?");