babel / kneden

Transpile ES2017 async/await to vanilla ES6 Promise chains: a Babel plugin
ISC License
514 stars 41 forks source link

Return not handled properly? #135

Open IngwiePhoenix opened 5 years ago

IngwiePhoenix commented 5 years ago

I have tried something out, and appearently a return value is not passed correctly.

Code:

async function someFunction() {
  return 1;
}

class Foo {
  async init() {
    try {
      var a = await someFunction();
      var b = await someFunction();
      var c = a+b;
      console.log("Math:" + (a+b))
      return c;
    } catch(e) {
      throw e;
    }
  }
}

(async ()=>{
  var e = new Foo()
  var f = await e.init()
  console.log("Res: "+f)
  return 0;
})();

Result:

"use strict";

function someFunction() {
  return Promise.resolve().then(function () {
    return 1;
  });
}

class Foo {
  init() {
    var a, b, c;
    return Promise.resolve().then(function () {
      return Promise.resolve().then(function () {
        return someFunction();
      }).then(function (_resp) {
        a = _resp;
        return someFunction();
      }).then(function (_resp) {
        b = _resp;
        c = a + b;
        console.log("Math:" + (a + b));
        return c;
      }).catch(function (e) {
        throw e;
      });
    }).then(function () {});
  }

}

(() => {
  var e, f;
  return Promise.resolve().then(function () {
    e = new Foo();
    return e.init();
  }).then(function (_resp) {
    f = _resp;
    console.log("Res: " + f);
    return 0;
  });
})();

What that outputs:

Math:2
Res: undefined

Expected:

Math:2
Res: 2 <--

Did I do something wrong in the code? I am not sure about it, so I thought I'd post it here.