Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect compilation of try-catch with async/await #423

Closed
in19farkt opened this issue Jan 9, 2020 · 7 comments · Fixed by #795
Closed

Incorrect compilation of try-catch with async/await #423

in19farkt opened this issue Jan 9, 2020 · 7 comments · Fixed by #795
Labels
kind: bug Something isn't working scope: upstream Issue in upstream dependency topic: async-to-promises Related to bugs with babel-plugin-async-to-promises

Comments

@in19farkt
Copy link

Current Behavior

My function

export async function getAccount(provider: Provider): Promise<string | null> {
  try {
    const accounts: string[] = await provider.send('eth_accounts'); // 1. provider.send returns undefined
    return accounts[0] || null; // 2. throw error 'Cannot read property '0' of undefined'
  } catch {
    warning('Some text'); // 3. log warning
  }

  return 'fallback account'; // 4. return 'fallback account'
}

compiled to

var getAccount = function getAccount(provider) {
  try {
    var _exit2 = false;

    var _temp2 = _catch(function () {
      return Promise.resolve(provider.send('eth_accounts')).then(function (accounts) { // 1. accounts === undefined
        _exit2 = true;
        return accounts[0] || null; // 2. throw error 'Cannot read property '0' of undefined'
      });
    }, function () {
      process.env.NODE_ENV !== "production" ? warning('Some text') : void 0; // 3. log warning
    });

    return Promise.resolve(_temp2 && _temp2.then ? _temp2.then(function (_result) {
      return _exit2 ? _result : 'fallback account'; // 4. return undefined because _exit2 === true and _result === undefined
    }) : _exit2 ? _temp2 : 'fallback account');
  } catch (e) {
    return Promise.reject(e);
  }
};

Expected behavior

compiled to

var getAccount = function getAccount(provider) {
  try {
    var _exit2 = false;

    var _temp2 = _catch(function () {
      return Promise.resolve(provider.send('eth_accounts')).then(function (accounts) { // 1. accounts === undefined
-       _exit2 = true;
        return accounts[0] || null; // 2. throw error 'Cannot read property '0' of undefined'
+     }).then(function () {
+       _exit2 = true;
      });
    }, function () {
      process.env.NODE_ENV !== "production" ? warning('Some text') : void 0; // 3. log warning
    });

    return Promise.resolve(_temp2 && _temp2.then ? _temp2.then(function (_result) {
-     return _exit2 ? _result : 'fallback account'; // 4. return undefined because _exit2 === true and _result === undefined
+     return _exit2 ? _result : 'fallback account'; // 4. return 'fallback account' because _exit2 === false
    }) : _exit2 ? _temp2 : 'fallback account');
  } catch (e) {
    return Promise.reject(e);
  }
};

Your environment

Software Version(s)
TSDX 0.12.0
TypeScript 3.7.2
npm/Yarn npm 6.12.0/yarn 1.21.1
Node v10.15.0
Operating System macOS Catalina 10.15.2
@ambroseus
Copy link
Contributor

@in19farkt this is not tsdx but babel-plugin-transform-async-to-promises issue
https://www.npmjs.com/package/babel-plugin-transform-async-to-promises

tsdx uses this plugin implicitly

name: 'babel-plugin-transform-async-to-promises',

@jaredpalmer

This comment has been minimized.

@jaredpalmer

This comment has been minimized.

@in19farkt
Copy link
Author

I duplicated the issue in the babel-plugin-transform-async-to-promises repository
rpetrich/babel-plugin-transform-async-to-promises#53

@agilgur5 agilgur5 added kind: bug Something isn't working scope: upstream Issue in upstream dependency labels Mar 9, 2020
@agilgur5 agilgur5 changed the title Incorrect compilation Incorrect compilation of try-catch with async/await Mar 9, 2020
@agilgur5 agilgur5 reopened this Mar 9, 2020
@agilgur5 agilgur5 added the topic: async-to-promises Related to bugs with babel-plugin-async-to-promises label Mar 20, 2020
@agilgur5
Copy link
Collaborator

@allcontributors please add @in19farkt for bug

@allcontributors
Copy link
Contributor

@agilgur5

I've put up a pull request to add @in19farkt! 🎉

@agilgur5
Copy link
Collaborator

agilgur5 commented Sep 20, 2020

async-to-promises has been replaced with babel-plugin-polyfill-regenerator in #795 and will be released in v0.14.0 soon. It will only pure polyfill generators for targets that need a polyfill according to your browserslistrc or preset-env targets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug Something isn't working scope: upstream Issue in upstream dependency topic: async-to-promises Related to bugs with babel-plugin-async-to-promises
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants