question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect compilation of try-catch with async/await

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
agilgur5commented, 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.

0reactions
allcontributors[bot]commented, Sep 20, 2020

@agilgur5

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

C# async await (try/)catch fails in .NET 6 [closed]
You apparently have a missing await somewhere in your code, which is causing an exception to be wrapped in an AggregateException .
Read more >
Capture error and data in async-await without try-catch
At first I thought it isn't a problem but as fate would have it I was working on chained API calls and the...
Read more >
Async await and Try catch in JavaScript | TechFerment
async/await in JavaScript allows developers to write asynchronous code in a way that looks and feels synchronous.
Read more >
Correcting Common Async/Await Mistakes in .NET - YouTube
Did you know that the .NET compiler turns our async methods into classes? And that .NET adds a try/catch block to each of...
Read more >
Error handling with Async/Await in JS | by Ian Segers | ITNEXT
Now we have the classic problem, thisThrows returns a rejecting promise, so the regular try...catch is not able to catch the error. As...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found