Async loop over object in javascript

Usually we can do loops for arrays and objects to iterate over properties / values. But the loops are blocking. However, timeouts can be used to simulate an asynchronization cycle. I managed to do this for an array .

//do stuff

(function asyncLoop(i){

    //do stuff in the current iteration

    if(++i < array.length){
        setTimeout(function(){asyncLoop(i);}, 1);
    } else {
        callback();
    }
}(0));

//do stuff immediately after, while looping

but this model only works during a loop in an array where there is a delimiter - iwhich is passed. is there any way to do this on an object? let me just say that the object has 50k keys for repetition, which makes it unreasonably long.

I already know about this setImmediate(afaik, only new IE) and WebWorkers (not yet in IE), but I just want to know if it is possible to use the same strategy for an object.

+3
1

, , , for (key in obj), , .

, , , . , , , .

Object.keys(obj) ES5 ( ES5, ), , ES5:

var keys = [];
for (var i in obj) {
    if (obj.hasOwnProperty(i)) {
        keys.push(i);
    }
}
+6

All Articles