MatAtBread / fast-async

605 stars 21 forks source link

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

Open coolaj86 opened 4 years ago

coolaj86 commented 4 years ago

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