babel / kneden

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

Doesn't allow to get a final result of an async function #45

Open xgrommx opened 8 years ago

xgrommx commented 8 years ago

Hello @marten-de-vries I have some experiments with your plugin and I found it interesting, but I have some cases when I don't get that I expected. For example I should to get sum of 100, 200, 300 but I cannot to get it.

const delay = (time, value) => new Promise((resolver, rejecter) =>
    setTimeout(() => resolver(value), time)
)

const f = async (x, y, z) => {
    const a = await delay(1000, x);
    console.log(a);
    const b = await delay(2000, y);
    console.log(b);
    const c = await delay(3000, z);
    console.log(c);

    return a + b + c;
}

f(100, 200, 300).then(sum => console.log(`Sum of two arguments is ${sum}`));
marten-de-vries commented 8 years ago

Thanks for your report. I'm afraid I can't reproduce it. Did you include babel-preset-es2015? It's a requirement when using other ES6/7 features as mentioned in the README file:

npm i babel-cli babel-plugin-async-to-promises babel-preset-es2015
marten@procyon:~/Bureaublad/testcase$ ./node_modules/.bin/babel --plugins async-to-promises --presets es2015 src.js -o index.js
marten@procyon:~/Bureaublad/testcase$ cat index.js 
"use strict";

var delay = function delay(time, value) {
    return new Promise(function (resolver, rejecter) {
        return setTimeout(function () {
            return resolver(value);
        }, time);
    });
};

var f = function f(x, y, z) {
    var a, b, c;
    return Promise.resolve().then(function () {
        return delay(1000, x);
    }).then(function (_resp) {
        a = _resp;

        console.log(a);
        return delay(2000, y);
    }).then(function (_resp) {
        b = _resp;

        console.log(b);
        return delay(3000, z);
    }).then(function (_resp) {
        c = _resp;

        console.log(c);

        return a + b + c;
    });
};

f(100, 200, 300).then(function (sum) {
    return console.log("Sum of two arguments is " + sum);
});
marten@procyon:~/Bureaublad/testcase$ node index.js 
100
200
300
Sum of two arguments is 600
marten@procyon:~/Bureaublad/testcase$ 
xgrommx commented 8 years ago

Did you mean that I need http://babeljs.io/docs/plugins/transform-regenerator/ ? But maybe it will be works without this module? For example I can use http://babeljs.io/docs/plugins/transform-async-to-generator/ or http://babeljs.io/docs/plugins/transform-async-to-module-method/ with bluebird or creed and get the same result as of your module, but I don't need to use regenerator

marten-de-vries commented 8 years ago

No, regenerator isn't necessary, but babel-preset-es2015 is. I can't reproduce the rest of Babel in this module, so this module only accepts ES5 with the addition of async/await. If your code isn't ES5, downcompile it by enabling that preset.

chpio commented 8 years ago

Hallo, im getting the same behavior. my code: https://github.com/ippm/ippm/blob/kneden-error/src/ippm-systemjs.js?ts=2 the babel config: https://github.com/ippm/ippm/blob/kneden-error/gulpfile.babel.js?ts=2#L38-L46 babel-preset-es2015-rollup is babel-preset-es2015 minus cjs transform plus external helpers

i looked into the generated code and there is a }).then(function () {}); at the end even though im returning stuff at the end of the function/method. If i delete the then at the end it starts working as expected.

chpio commented 8 years ago

sorry, should have posted it in #21

ps: it would be nice if it would use the babel-runtime (if enabled) Promise and not rely on a global Promise.

ljharb commented 8 years ago

If you use babel-runtime, its Promise is the global Promise.

chpio commented 8 years ago

I have no idea what you meant by that. the babel-transform-runtime plugin converts all "Promise" into a ponyfill (namespaced/"localized" polyfill) which is loaded from the babel-runtime package.

kneden is using the global Promise: https://ipfs.io/ipfs/QmTCvF2Ki7SHPUAABHpQZ8HavWkAvooUZbFmy3Qjz4WrDs

the issue is maybe that kneden is executed after babel-tranform-runtime https://github.com/marten-de-vries/kneden/issues/19#issuecomment-183875457

Although it does take care to run after the es2015 preset so it can be used in conjunction with it

btakita commented 7 years ago

Any movement on this issue?