<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>25048</bug_id>
          
          <creation_ts>2014-03-14 02:14:04 +0000</creation_ts>
          <short_desc>consider whether an IDL attribute of type Promise&lt;T&gt; should catch exceptions and wrap them up as a rejected Promise like they are for operations</short_desc>
          <delta_ts>2017-01-20 08:24:51 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebAppsWG</product>
          <component>WebIDL</component>
          <version>unspecified</version>
          <rep_platform>PC</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Cameron McCormack">cam</reporter>
          <assigned_to name="Cameron McCormack">cam</assigned_to>
          <cc>annevk</cc>
    
    <cc>bzbarsky</cc>
    
    <cc>d</cc>
    
    <cc>mike</cc>
    
    <cc>public-script-coord</cc>
    
    <cc>tobie.langel</cc>
          
          <qa_contact>public-webapps-bugzilla</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>102342</commentid>
    <comment_count>0</comment_count>
    <who name="Cameron McCormack">cam</who>
    <bug_when>2014-03-14 02:14:04 +0000</bug_when>
    <thetext></thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102344</commentid>
    <comment_count>1</comment_count>
    <who name="Boris Zbarsky">bzbarsky</who>
    <bug_when>2014-03-14 02:20:37 +0000</bug_when>
    <thetext>Are there use cases for attributes of Promise type where this would make sense?  They wouldn&apos;t be creating a new Promise each time, so it&apos;s not like the Promise would represent a operation the attribute getter triggered....</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102345</commentid>
    <comment_count>2</comment_count>
    <who name="Domenic Denicola">d</who>
    <bug_when>2014-03-14 02:24:38 +0000</bug_when>
    <thetext>In general, anytime someone is expecting to interface with an asynchronous API, they are expecting asynchronous errors, which are caught via `.catch(e =&gt; ...)`. It is very surprising if a synchronous error is thrown. If an API is known to cause both types of errors, you need both `.catch(e =&gt; ...)` and `catch(e) { ... }`, which is frustrating.

So yes, I think this change would make sense. I&apos;m not sure under what situations it would get triggered---how exception-prone are the operations performed by getters, usually?---but philosophically, it&apos;s definitely desirable.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102346</commentid>
    <comment_count>3</comment_count>
    <who name="Cameron McCormack">cam</who>
    <bug_when>2014-03-14 02:26:46 +0000</bug_when>
    <thetext>It&apos;s not so common for IDL attributes to throw exceptions on getting, but not unheard of.

Hmm, what if you had an IDL attribute of a Promise&lt;T&gt; type that throws when assigning?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102347</commentid>
    <comment_count>4</comment_count>
    <who name="Boris Zbarsky">bzbarsky</who>
    <bug_when>2014-03-14 02:27:40 +0000</bug_when>
    <thetext>The question is whether there are ever use cases in which a promise attribute is is actually representing asynchronous API.

Or to put this another way: are there any use cases for allowing attributes of Promise type at all?

&gt; how exception-prone are the operations performed by getters, usually?

It depends on the getter.  The most common practical source of exceptions is if you .call() the getter function on a wrong kind of object.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102348</commentid>
    <comment_count>5</comment_count>
    <who name="Domenic Denicola">d</who>
    <bug_when>2014-03-14 02:34:27 +0000</bug_when>
    <thetext>&gt; Or to put this another way: are there any use cases for allowing attributes of Promise type at all?

For sure! See all the usage in [1] or [2]. Or, the recently-proposed loaded/ready promises could easily be attributes, if not for the fact that most of those resources allow you to reload them (e.g. by setting `.src` again).

&gt; The most common practical source of exceptions is if you .call() the getter function on a wrong kind of object.

This should return a rejected promise for the same reason; if you are using a getter representing an asynchronous result, you should expect to be able to do asynchronous error handling.

&gt; Hmm, what if you had an IDL attribute of a Promise&lt;T&gt; type that throws when assigning?

Yeah, this is trickier to reason about---mainly because I don&apos;t know of a reasonable use case for promise attributes with setters. I assume there&apos;d be coercion behavior, so e.g. for a Promise&lt;long&gt; attribute these would be fine:

foo.bar = 5;
foo.bar = Promise.resolve(5);

but these would not:

foo.bar = {};
foo.bar = Promise.resolve(function () { });

I would probably again argue that the result should be `foo.bar` set to a promise rejected with a `TypeError`, since people would access the result of the set via

foo.bar.then(processValueRepresentedByBar, couldNotGetValue);

and would be surprised if they had to write

try {
  foo.bar.then(getValueThatWasSet, couldNotGetValue);
} catch (e) {
  couldNotGetValue(e);
}


[1]: https://github.com/whatwg/streams
[2]: https://rawgithub.com/ClaesNilsson/raw-sockets/gh-pages/index.html#interface-tcpsocket</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102349</commentid>
    <comment_count>6</comment_count>
    <who name="Cameron McCormack">cam</who>
    <bug_when>2014-03-14 02:37:18 +0000</bug_when>
    <thetext>(In reply to Domenic Denicola from comment #5)
&gt; &gt; Hmm, what if you had an IDL attribute of a Promise&lt;T&gt; type that throws when assigning?
&gt; 
&gt; Yeah, this is trickier to reason about---mainly because I don&apos;t know of a
&gt; reasonable use case for promise attributes with setters. I assume there&apos;d be
&gt; coercion behavior, so e.g. for a Promise&lt;long&gt; attribute these would be fine:
&gt; 
&gt; foo.bar = 5;
&gt; foo.bar = Promise.resolve(5);
&gt; 
&gt; but these would not:
&gt; 
&gt; foo.bar = {};
&gt; foo.bar = Promise.resolve(function () { });
&gt; 
&gt; I would probably again argue that the result should be `foo.bar` set to a
&gt; promise rejected with a `TypeError`, since people would access the result of
&gt; the set via
&gt; 
&gt; foo.bar.then(processValueRepresentedByBar, couldNotGetValue);

That&apos;s my initial feeling too.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>102350</commentid>
    <comment_count>7</comment_count>
    <who name="Boris Zbarsky">bzbarsky</who>
    <bug_when>2014-03-14 03:26:54 +0000</bug_when>
    <thetext>OK, I guess if you only have one promise representing your state then it makes sense to make it an attribute. But then I&apos;m not sure it makes sense to have that attribute create-and-return promises when called with the wrong this object.

Put another way, I strongly feel like for attribute getters we should preserve the invariant that getter.call(obj) == getter.call(obj) tests true.

So I would argue we should require that getters that return promises not throw normally and allow them to throw if invoked on an object that&apos;s the wrong type.  In practice, I don&apos;t expect anyone to be doing that sort of thing anyway.

As far as setters, if we have no use cases, let&apos;s not add complexity and just disallow them.  WebIDL already has too many unused (and never will be used!) things that just make it more complex than it should be.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>127756</commentid>
    <comment_count>8</comment_count>
    <who name="Domenic Denicola">d</who>
    <bug_when>2016-10-13 13:37:19 +0000</bug_when>
    <thetext>(In reply to Boris Zbarsky from comment #7)
&gt; OK, I guess if you only have one promise representing your state then it
&gt; makes sense to make it an attribute. But then I&apos;m not sure it makes sense to
&gt; have that attribute create-and-return promises when called with the wrong
&gt; this object.
&gt; 
&gt; Put another way, I strongly feel like for attribute getters we should
&gt; preserve the invariant that getter.call(obj) == getter.call(obj) tests true.

I disagree that this invariant applies here. Its synchronous analogue is saying that getter.call(obj), when it fails, should always throw the same error. It&apos;s OK to return a different promise in the failure cases.

&gt; As far as setters, if we have no use cases, let&apos;s not add complexity and
&gt; just disallow them.  WebIDL already has too many unused (and never will be
&gt; used!) things that just make it more complex than it should be.

Sounds good to me to disallow promise types with setters.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>127757</commentid>
    <comment_count>9</comment_count>
    <who name="Boris Zbarsky">bzbarsky</who>
    <bug_when>2016-10-13 14:03:52 +0000</bug_when>
    <thetext>&gt; Its synchronous analogue is saying that getter.call(obj), when it fails,
&gt; should always throw the same error.

This argument basically says to me that the basic idea of getters with Promise return values is wacky and we should be using a method instead...  But I accept that others disagree on this.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>127763</commentid>
    <comment_count>10</comment_count>
    <who name="Anne">annevk</who>
    <bug_when>2016-10-13 15:06:36 +0000</bug_when>
    <thetext>If you cannot depend on === for promises due to the error case, do promises for IDL attributes need to ban [SameObject]?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>127764</commentid>
    <comment_count>11</comment_count>
    <who name="Boris Zbarsky">bzbarsky</who>
    <bug_when>2016-10-13 15:09:31 +0000</bug_when>
    <thetext>That really depends on the meaning people imbue [SameObject] with.  The ambiguity is pretty unfortunate, I agree.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>128399</commentid>
    <comment_count>12</comment_count>
    <who name="Tobie Langel">tobie.langel</who>
    <bug_when>2017-01-20 08:24:51 +0000</bug_when>
    <thetext>Fixed in https://github.com/heycam/webidl/commit/4c9c298</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>