Skip to content
This repository has been archived by the owner on Jun 10, 2019. It is now read-only.

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

Open
xgrommx opened this issue Mar 26, 2016 · 8 comments
Open

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

xgrommx opened this issue Mar 26, 2016 · 8 comments

Comments

@xgrommx
Copy link

xgrommx commented Mar 26, 2016

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
Copy link
Collaborator

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
Copy link
Author

xgrommx commented Mar 26, 2016

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
Copy link
Collaborator

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
Copy link

chpio commented Jun 17, 2016

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
Copy link

chpio commented Jun 17, 2016

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
Copy link
Member

ljharb commented Jun 17, 2016

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

@chpio
Copy link

chpio commented Jun 17, 2016

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
#19 (comment)

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

@btakita
Copy link

btakita commented Mar 2, 2017

Any movement on this issue?

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

No branches or pull requests

5 participants