Topic: Asynchronous For-In Loops in Firefox Pages that link to <a href="https://ozoneasylum.com/backlink?for=30978" title="Pages that link to Topic: Asynchronous For-In Loops in Firefox" rel="nofollow" >Topic: Asynchronous For-In Loops in Firefox\

 
Author Thread
DavidJCobb
Nervous Wreck (II) Inmate

From: United States
Insane since: Mar 2009

IP logged posted posted 04-29-2009 21:26 Edit Quote

This isn't cross-browser, but it is interesting nonetheless, and in any case, I haven't seen it posted anywhere else... Or mentioned anywhere else... Or listed in Google...

Anyway, it's possible to write an asynchronous for-in loop using a new feature of JavaScript 1.7: a FF2+ object called Iterator. Click here to read about how it works.

Anyway, here's a code for an object-based, Iterator-using, self-contained asynchronous looping mechanism.

code:
AsyncLooper = 
   {
      go:
         function(obj,iCallback,eCallback) {
            AsyncLooper.loop(obj,iCallback,eCallback,Iterator(obj));
         },
      loop:
         function(o,i,e,I) {
            var prop;
            for(var counter=0;counter<21;counter++) {
               try { // only way to check if there aren't any more properties
                  prop=I.next();
               } catch (e) { // if no more properties
                  return e(o)
               }
               i(o,p[0],p[1]);
            }
            window.setTimeout(AsyncLooper.loop,1,o,i,e,I);
         }
   };

// And a usage example:
myPointlessObject = {a:123,b:456,c:789,e:345,d:012};
STRING="myPointlessObject == "+myPointlessObject.toSource();
AsyncLooper.go(myPointlessObject,
   function(o,n,v) {
      STRING+="\nmyPointlessObject['"+n+"'] == "+v
   },
   function() {
      alert(STRING);
   }
);



To use it, you would call AsyncLooper.go() with the following arguments:
obj == The object to loop through.
iCallback == A function to be called on each iteration. It receives the object, property name, and property value as arguments.
eCallback == A function to be called when the loop ends. It receives the object as an argument.

And there you have it: an asynchronous for-in loop.

As a side note, it's possible to iterate through an Iterator using a for-in loop, but breaking out of the for-in loop kills the iterator (future for-in loops on it won't run, and any future calls to its next() method generate a StopException).

code:
// Assume that myIterator is an Iterator.
for(i in myIterator) {
   break;
}
for(i in myIterator) {
   // even if the break statement
   // above ran before the Iterator
   // was "finished", this code will
   // never run...
}
myIterator.next();
// and calls to its next() method
// cause StopExceptions.



----------------------

coach
Nervous Wreck (II) Inmate

From:
Insane since: May 2011

IP logged posted posted 05-31-2011 11:08 Edit Quote
Edit TP: spam removed


Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu