When the only tool you have is subclassing, all extensions look like....

C. Scott Ananian ecmascript at cscott.net
Tue Jun 9 16:47:00 UTC 2015


On Tue, Jun 9, 2015 at 12:38 PM, Mark S. Miller <erights at google.com> wrote:

> On Tue, Jun 9, 2015 at 9:29 AM, C. Scott Ananian <ecmascript at cscott.net>
>  wrote:
>
>> Mark: The `prfun` library in fact uses `Promise#timeout(n)` instead of a
>> `TimeoutPromise` subclass.  But this is really a language-design question.
>> You might as well ask why we have `WeakMap()` as a constructor instead of
>> using `Map#weak()` or `weakmapify(map)`.  The fundamental reason is "so you
>> can name (and thus test) the type of the object".
>>
>
> Do you ever test that the object returned by `Promise#timeout(n)` is
> something other than a plain promise?
>

As I said, I use `Promise#timeout` myself in my implementation, not a
subclass.  So I haven't written tests for TimeoutPromise myself.

But the smalltalk community has a number of places where it is worth
testing whether an argument is a WeakArray.  Those examples would translate
directly to tests for WeakPromise.

And for TimeoutPromise you could imagine something like:
```
var protect = function(x) {
  return (x instanceof TimeoutPromise) ? x : TimeoutPromise.resolve(x);
};
var doSomethingImportant = function(x) {
  // ensure that we're not going to hang forever waiting for x
  return protect(x).catch(....).then(....);
};
```

It is rather convenient that the `protect` function already has a clever
name: `TimeoutPromise.resolve`.
  --scott
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20150609/09af7ee8/attachment.html>


More information about the es-discuss mailing list