tc39 / proposal-do-expressions

Proposal for `do` expressions
MIT License
1.12k stars 14 forks source link

support for try/catch expressions: #6

Open graingert opened 7 years ago

graingert commented 7 years ago

I expect this to work:

const foo = () => do { throw new Error('foo'); };
const bar = () => 3;
const res = do {
  try { foo(); } catch (e) { bar(); }
}
console.log(res); // 3;

But I get undefined;

babel transpiles to:

const foo = () => function () {
  throw new Error('foo');
}();
const bar = () => 3;
const res = function () {
  try {
    return foo();
  } catch (e) {
    bar();
  }
}();
console.log(res); // 3;

but I expected:

const foo = () => function () {
  throw new Error('foo');
}();
const bar = () => 3;
const res = function () {
  try {
    return foo();
  } catch (e) {
    return bar();
  }
}();
console.log(res); // 3;

an alternative that works is:

const foo = () => do { throw new Error('foo'); };
const bar = () => 3;
const res = do { try { foo(); } catch (e) {} } || bar();
console.log(res); // 3;
graingert commented 7 years ago

I'd also like to see an implicit catch { } on try expressions.

const foo = () => do { throw new Error('foo'); };
const bar = () => 3;
const res = do { try { foo(); } } || bar();
console.log(res); // 3;
claudepache commented 7 years ago

@graingert This is a bug in Babel, your expectation is correct.

As for the “implicit catch” wish, this is unrelated to this proposal. I’ll just mention that it has been recently discussed on es-discuss: https://esdiscuss.org/topic/an-operator-for-ignoring-any-exceptions

Jessidhia commented 7 years ago

What should happen if there is a finally block? If it is anything but an empty block, it potentially overrides the completion value of the try or catch block, unless any potential completion value of finally is always ignored.

claudepache commented 7 years ago

@Kovensky According to the spec, completion value of a finally block is ignored, unless it is an abrupt completion.