Use ".then(null, Cu.reportError);" with Promise.jsm

David Rajchenbach-Teller dteller at mozilla.com
Mon Aug 12 13:28:01 UTC 2013


On 8/10/13 5:04 AM, Mark Hammond wrote:
> On 9/08/2013 8:32 PM, David Rajchenbach-Teller wrote:
>> Furthermore, I *strongly* suggest that we r- any promise chain that does
>> not report errors, unless there is a comment explaining why we do not.
> 
> This concept of a "promise chain" escapes me.  I understand that
> conceptually, promise code should look like:
> 
>   something.then(step1)
>            .then(step2)
>            .then(step3)
>            .then(null, Cu.reportError)
> 
> One nice promise chain and the final .then() looks reasonable.  But
> every time I try and use promises, they actually end up looking like:
> 
>   something.then(result => {
>     step1.then(result => {
>       step2.then(result => {
>         step3.then(result => {
>         });
>       });
>     });
>   })
> 
> in that case we have 4 "promise chains".  Thus, we need 4 extra
> .then(null, Cu.reportError) calls, making the code much larger and
> harder to follow.

That's actually a single chain that promises to return the result of
     step3.then(result => { ... })
so, with respect to error reporting, fine with me.

Of course, you can as well rewrite this

Task.spawn(function() {
  try {
    let result1 = yield something();
    let result2 = yield step1();
    let result3 = yield step2();
    let result = yield step3();
  } catch (ex) {
    Cu.reportError(ex);
  }
});

I'd favor the latter form over the former.

I believe that this also answers the rest of your post.

Cheers,
 David


-- 
David Rajchenbach-Teller, PhD
 Performance Team, Mozilla



More information about the firefox-dev mailing list