Hi,

> I looked at the Function#defer code, and there was nothing about
> stack.

The stack in question is in the JavaScript interpreter, not Prototype.
In fact, it even says "...the interpreter's call stack." :-) (It's
probably more a queue than a stack.)

JavaScript in web browsers is single-threaded. That means that when
code is *actively running*, no other code (for instance, scheduled via
setTimeout or from an event handler) can run. Requests to run that
code queue up in the interpreter until it's idle. That's the effect
that Function#defer takes advantage of, by scheduling the code to run
via setTimeout after a very brief delay.

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com


On Apr 1, 7:36 am, mot <[email protected]> wrote:
> Hello all,
> I use prototype library for several years and like it very much.
>
> The question is about Function#defer. I used it in my work but never
> looked under the hood.
> The description says: "A “deferred” function will not run immediately;
> rather, it will run as soon as the interpreter’s call stack is empty."
>
> I looked at the Function#defer code, and there was nothing about
> stack. All that defer do is delaying function run for 0.01 second. And
> there is nothing about such behaviour in the docs.
>
> According to docs, such code:
> function a1(text) {
>         console.log(text);
>
> }
>
> a1.defer(1);
> a1(2);
>
> Should produce '1' and then '2'. In real live it will produce '2' and
> then '1'.
>
> I think API docs should be updated to help developers better
> understand what defer really do.

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to