javascript - Side effects when async operating on arrays -
i'm learning node.js atm, i'm asking myself:
how "threadsafe" normal arrays?
example:
var myarr = ["alpha", "beta", "gamma", "delta"]; ee.on('event', function(itemstring) { //loop on array change length while looping through for(var i=0; i<myarr.length; i++) { // delete item out of array if(myarr[i] == itemstring) myarr.splice(i,1); } });
if multiple of events fired on ee-object, there chance, loop fail because indexes spliceed away?
or said different: way ensure loop won't skip or fail because elements may deleted callback call of same event?
thx :)
node.js
single threaded , not interrupts sync execution.
still, you're modifying array while iterating length may lead skipping elements. also, event not prepared fired twice same array element.
Comments
Post a Comment