0

Different notation of the same expression - not the same behaviour?

  1. This will toss Uncaught (in promise) v8 for you,

and I think it shouldn't! You may refresh on mdn://Promise/catch and гост://ecma2015.

var a = new Promise((resolve, reject) => setTimeout(_ => reject('v8'), 0));
a.catch(a => 1);
a.finally(a => 1);
  1. Basically the same code, but works as expected.

var a = new Promise((resolve, reject) => setTimeout(_ => reject('v8'), 0))
  .catch(a => 1);
a.finally(a => 1);

!! There must be at least one .finally or .then chained, else example #1 will not toss that Uncaught (in promise).

Reproduced in the latest versions of FF and Chromium (though I did not test this with servo yet).

  • .catch callback is executed in example #1.
  • .setTimeout, 0 just to position reject to top of call stack, timeout could be more than 0 anyways.

Can someone please explain, what did I miss? Is it just me or JavaScript?

Barmar
  • 741,623
  • 53
  • 500
  • 612
animaacija
  • 170
  • 9
  • 25
  • 1
    Doesn't look like a bug to me. #1 and #2 assign different values to a. – user2263572 Mar 04 '20 at 22:33
  • 2
    In #2 you're assigning the result of `.catch()` to `a`, not the original promise. – Barmar Mar 04 '20 at 22:34
  • I object! `new Promise(() => {}).catch(_=>1) instanceof Promise // true` and `new Promise(() => {}) instanceof Promise // true`. Did you also try both snippets without a `finally` block? – animaacija Mar 04 '20 at 22:41
  • @user2263572 @Barmar in specification link it states `.catch()` will return `this`. Or maybe I misunderstood? – animaacija Mar 04 '20 at 23:01
  • 3
    In the first example, you are using Promise **branching**, which will give different results from Promise **chaining** – blex Mar 04 '20 at 23:11
  • Thank you @blex, so when I do branching, it still the same confusion I get: `var a = new Promise((a,r) => r('v8'), 1000); a.catch(a => 1); //a is rejected promise, NO ERROR` and `var a = new Promise((a,r) => r('v8'), 1000); a.catch(a => 1); a.then(a => 1); // existence of this gives ERROR` I will dig a bit about keywords you shared thanx! – animaacija Mar 04 '20 at 23:41

0 Answers0