W3C home > Mailing lists > Public > www-dom@w3.org > April to June 2013

[DOM Futures] Is there a typo in future wrapper callback?

From: Sean Hogan <shogun70@westnet.com.au>
Date: Wed, 15 May 2013 23:37:46 +1000
Message-ID: <51938FAA.3000104@westnet.com.au>
To: www-dom@w3.org
CC: Anne van Kesteren <annevk@annevk.nl>
(Posting this again because my copy-and-paste inserted hyperlinks the 
first time)

I'm perplexed by the second step of future wrapper callbacks:

Set callback's `callback this value` to resolver's associated future.

Assuming this results in the equivalent of:

     callback.call(future, value)

then I can't work out the purpose.
Is there a use for calling `future.then()`, etc inside the callback?

There is also a potential failure mode in the unlikely case that the 
callback returns `this`, that is, returns the current future object. The 
result would be the following:

     future.then(function() { return this; }); // returns future

     run the associated resolver's resolve() with future

     future has a `then` method so it is called with callbacks that 
would run future's resolve and reject algorithms

     now future is still pending until those callbacks get called, but 
they won't get called until future is resolved. Obviously this stalls 
permanently.

Again this scenario is unlikely, but it is another indicator to me that 
the spec is wrong (has a typo?)

My first thought was that the step was supposed to read:

     Set callback's `callback this value` to resolver.

But that doesn't make sense either because the callback is supposed to 
cause the future to be resolved by returning a value or throwing an 
error (or by returning a **separate** Future).

However, I do think this behavior would be useful.

Imagine a method of Future - say Future.prototype.whenever - which 
behaves like `then`, except that callbacks are called as:

     callback.call(resolver, value);

This would make asynchronous pipes straight-forward. I'd better give an 
example:

     Future.accept(startValue)
     .whenever(function(value) {
         var r = this;
         setTimeout(function() {
             r.accept(2*value);
         });
     })
     .whenever(function(value) {
         var r = this;
         setTimeout(function() {
             r.accept(value + 8);
         });
     })
     .done(function(value) {
         console.log(value);
     });

The callbacks can behave similarly to the `init` function called from 
`new Future(init)`.
If the `init` function was also called in this way then the concept 
would be easier to grasp.
Received on Wednesday, 15 May 2013 13:38:21 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 20 October 2015 10:46:20 UTC