tc39 / proposal-do-expressions

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

Is this a syntax sugar for iife? #29

Closed lxe closed 6 years ago

lxe commented 6 years ago
let x = do {
  if (foo()) { f() }
  else if (bar()) { g() }
  else { h() }
};

Can be expressed as

let x = (() => {
  if (foo()) { return f() }
  else if (bar()) { return g() }
  else { return h() }
})();

Right?

ljharb commented 6 years ago

The IIFE case can’t return from the enclosing function, and creates an extra frame on the stack (ie, if an error is thrown), and vars defined within it wouldn’t be available outside the expression.

lxe commented 6 years ago

Thanks for the explanation @ljharb I ... didn't think of the block scope preservation.