babel / kneden

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

Setting local variables inside the promise not outside #79

Closed selfrefactor closed 7 years ago

selfrefactor commented 7 years ago

So the issue is the following -

This code

async function mainAsync(url) {
    let willReturn = {}
    willReturn.browserPerf = await browserPerf.main(url)
    if (!url.includes("localhost")) {
        willReturn.pageSpeedInsights = await pageSpeedInsights.main(url)
        willReturn.webPageTest = await webPageTest.main(url)
    }
    return willReturn
}

is compiled to

function mainAsync(url) {
    return Promise.resolve().then(function () {
        let willReturn = {};
        return browserPerf.main(url);
    }).then(function (_resp) {
        willReturn.browserPerf = _resp;
        if (!url.includes("localhost")) {
            return Promise.resolve().then(function () {
                return pageSpeedInsights.main(url);
            }).then(function (_resp) {
                willReturn.pageSpeedInsights = _resp;
                return webPageTest.main(url);
            }).then(function (_resp) {
                willReturn.webPageTest = _resp;
            });
        }
    }).then(function () {
        return willReturn;
    });
}

The problem is that the line of defining willReturn is inside the promise and this blocks execution of the code one it reaches _willReturn.browserPerf = _resp_

The line * let willReturn = {};* should be the first line of the function body, not the second. Otherwise the library is great, congratulations on that.

gerkirill commented 7 years ago

I believe the issue is caused by let used instead of var. In readme you can find the following:

Note: Kneden only supports transpiling ES5 with the addition of async/await. If you're using other ES6 features (like arrow functions, let/const, classes, etc.), make sure you transpile them down to valid ES5 code first using the babel es2015 preset.

I've tried to use var instead, and the 1st line of the function generated become var willReturn;

selfrefactor commented 7 years ago

Thank you for your response. So at the end is my error I didn't read the documentation more seriously, sorry for opening this issue.