Bringing setTimeout to ECMAScript

David Bruant david.bruant at labri.fr
Sat Mar 19 14:38:21 PDT 2011


Le 19/03/2011 21:05, Mark S. Miller a écrit :
> On Sat, Mar 19, 2011 at 10:57 AM, David Bruant <david.bruant at labri.fr
> <mailto:david.bruant at labri.fr>> wrote:
>  
>
>     I think there would be a need to wrapped the passed callback in
>     order to achieve garbage collection.
>
>
> I didn't understand that. Could you expand? Thanks.
It was just an implementation detail.
Actually it is easier to explain by coding it. So here it is:
https://gist.github.com/877797
What I was talking about is at line 11: To perform garbage collection,
there is a need to explicitely remove the reference we have of the
identity object. Otherwise, we take the risk to keep the reference
during the entire program lifetime.
I would have prefered to use a WeakMap for the "timeoutIdentityObjects"
variable but it would be compatible with no current ES implementation,
so a plain old array will do the job.
Anyway, this gist could be a start (a couple of cases aren't covered,
see comments) for your SES implementation. If you want it, it's all
yours :-)


>  
>
>
>
>     After giving it further thought, here are the ideas I've had on
>     adding time to the Q API:
>     Q.when(timedPromise, success); would be a nice syntax to call a
>     'success' function when the promise is resolved.
>
>
> I don't understand. Given that timedPromise is the kind of thing that
> delay() returns
Oooh. That wasn't the assumption I was making; I hadn't fully understood
the delay function. But after re-reading the delay function, then yes,
that's an excellent idea.

The only thing that I said afterward and that matter now is this
(corrected):
Something that could be convenient as the first argument of delay
(Q.delay?) would be to be allow a Date object. If I want a delayed
promise to be resolved when my next birthday happens, I would prefer to
write Q.delay( new Date(2012, 3, 8, 12) ) instead of computing
milliseconds since 1/1/70.
A decision would have to be made for dates in the past (actually, for
negative number of milliseconds (or the chosen time unit) too). Solving
them right away? Rejecting them right away?


> , just doing a Q.when() on a timePromise as above will already do the
> right thing. That's why the delay() and race() abstractions compose so
> nicely -- because race() does a Q.when on the promise returned by delay().
Things are getting more and more clear on promises. Now that I
understand the line:
var answer = race(bob ! foo(carol), delay(5000, Q.reject(new
Error("timeout"))));
I find it extremely elegant, indeed.

David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20110319/9047897a/attachment.html>


More information about the es-discuss mailing list