rpetrich / babel-plugin-transform-async-to-promises

Transform async/await to somewhat idiomatic JavaScript promise chains
MIT License
246 stars 17 forks source link

Named async function expression returning a variable value does not return a promise #50

Closed scq closed 2 years ago

scq commented 4 years ago

Minimal repro case:

const foo = async function _foo() {
  return bar;
};

Output:

const foo = function _foo() {
  try {
    return bar;
  } catch (e) {
    return Promise.reject(e);
  }
};

return bar() will also reproduce it.

Deleting const foo = (changing from a function expression to a function declaration), removing the name of the function, or returning a constant value (e.g. 3) will cause it to behave correctly.

scq commented 4 years ago

Turning on inlineHelpers also seems to resolve it.

scq commented 4 years ago

Similar issue here:

const foo = async function _foo(arg) {
  if (arg.length === 0) {
    return;
  }
};

Becomes:

const foo = function _foo(arg) {
  try {
    if (arg.length === 0) {
      return;
    }

    return (0, _helpers._await)();
  } catch (e) {
    return Promise.reject(e);
  }
};

Like before, inlineHelpers resolves it.

niksy commented 4 years ago

I can confirm the bug, last version without the bug is 0.8.13.