Killing `Promise.fulfill`

Mark S. Miller erights at google.com
Thu Aug 22 09:29:05 PDT 2013


On Thu, Aug 22, 2013 at 9:22 AM, Brandon Benvie <bbenvie at mozilla.com> wrote:

> On 8/22/2013 8:04 AM, Mark S. Miller wrote:
>
>>     var Promise = (function(){
>>         "use strict"; // of course
>>
>>         var brand = new WeakMap();
>>
>>         // only ever called with "new"
>>         function HiddenPromiseConstructor(**callback) {
>>             // initialize "this", which it can assume starts fresh and
>> trustworthily uninitialized
>>             brand.set(this, true);
>>         }
>>
>>         function Promise(arg) {
>>             if (Object.getPrototypeOf(this) === Promise.prototype &&
>> !(brand.has(this))) {
>>                 // assume likely called with "new", but do not trust
>> "this"
>>                 return new HiddenPromiseConstructor(arg)
>>             } else {
>>                 // assume called for coercion behavior. Ignore this
>>                 if (brand.has(arg)) {
>>                     return arg;
>>                 } else {
>>                     return Promise.of(arg);
>>                 }
>>             }
>>         }
>>         HiddenPromiseConstructor.**prototype = Promise.prototype;
>>
>>         // initialize Promise.prototype
>>         // initialize Promise statics
>>         return Promise
>>     })();
>>
>
> I'd make one small change to this:
>
>
>     if (Object.getPrototypeOf(this) === Promise.prototype &&
> !(brand.has(this))) {
>
> to
>
>     if (this instanceof Promise && !(brand.has(this))) {
>
> or an alternative that I think is functionally identical, if you want to
> avoid instanceof:
>
>     if (Promise.prototype.**isPrototypeOf(this) && !(brand.has(this))) {
>
>
> This change would allow subclassing of Promise.


This change would mislead my example

    Object.create(p, {Promise: {value: Promise}}).Promise(....)

into calling Promise's construct behavior instead of its coerce behavior.


>
>
> ______________________________**_________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/**listinfo/es-discuss<https://mail.mozilla.org/listinfo/es-discuss>
>



-- 
    Cheers,
    --MarkM
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20130822/65e8ca0b/attachment.html>


More information about the es-discuss mailing list