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

How to translate async/await to built-in Promise? #68

Open
coolaj86 opened this issue Oct 2, 2019 · 0 comments
Open

How to translate async/await to built-in Promise? #68

coolaj86 opened this issue Oct 2, 2019 · 0 comments

Comments

@coolaj86
Copy link

coolaj86 commented Oct 2, 2019

I'm looking for something that, with any version of babel, can simply translate async/await into Promises in a human-readable form - no any extra helpers or prototypes, etc.

Example In:

async function doStuff() {
  var x = await foo();
  var y = await bar(x);
  return y;
}

console.log(await doStuff());

Example Out:

function doStuff() {
  return Promise.resolve().then(function () {
    return foo().then(function ($await_0) {
      return bar($await_0).then(function ($await_1) {
        console.log($await_1);
      });
  });
});

doStuff().then(function ($await_0) {
    console.log($await_0);
});

Is this tool capable of that?

I've been trying the different examples in the README, but even with promise: true it seems that $asyncbind and trampoline, etc get added.

Also, it seems to use new Promise(function ($return, $error) {}) rather than Promise().resolve(function () {}), which seems like it would lead to thrown errors not being caught. Or maybe async/await doesn't actually handle errors the way I thought it does...

Apparently new Promise(function (resolve, reject) { ... }) catches exceptions now... or maybe it always did and I'm getting confused about it's inability to handle a returned promise with a rejection

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

No branches or pull requests

1 participant