Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix corner case in awaits #5158

Merged
merged 1 commit into from Oct 29, 2021
Merged

fix corner case in awaits #5158

merged 1 commit into from Oct 29, 2021

Conversation

alexlamsl
Copy link
Collaborator

fixes #5157

@kzc
Copy link
Contributor

kzc commented Oct 29, 2021

Is AST_Yield subject to any similar return optimizations?

@alexlamsl alexlamsl merged commit eb93d92 into mishoo:master Oct 29, 2021
@alexlamsl alexlamsl deleted the issue-5157 branch October 29, 2021 15:33
@alexlamsl
Copy link
Collaborator Author

IIRC the analogue of await-on-async would be yield *-on-generator, as yield would not expand on its value.

console.log(function*() {
    try {
        return yield* function*() {
            throw "FAIL";
        }();
    } catch (e) {
        return "PASS";
    }
}().next().value);
$ cat test.js | node
PASS
$ uglifyjs test.js -mc | node
PASS

@kzc
Copy link
Contributor

kzc commented Oct 29, 2021

IIRC the analogue of await-on-async would be yield *-on-generator, as yield would not expand on its value.

Yes, indeed.

esbuild is useful in understanding and debugging async/await in that regard...

$ cat a.js
async function fail() { 
    throw new Error("PASS");
} 
async function test() { 
    try { 
        return await fail(); 
    } catch (e) { 
        console.log(e.message);
    } 
}
test();
$ cat a.js | esbuild --target=es6
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
function fail() {
  return __async(this, null, function* () {
    throw new Error("PASS");
  });
}
function test() {
  return __async(this, null, function* () {
    try {
      return yield fail();
    } catch (e) {
      console.log(e.message);
    }
  });
}
test();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Minifying return await removes await which produces a different program
2 participants