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.

breaks are transpiled to return inside then

See original GitHub issue

Current Behavior

While working on my code I noticed a bug that led to my transpiled files and I saw that tsdx was incorrectly transforming my break inside switch cases to return. I thoughts that was rather odd so I created a sample project with the following code to pinpoint the issue:

function wait() {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      resolve();
    }, 1000);
  });
}

export const test = async () => {
  await wait();
  switch ('' as any) {
    case 'a':
      break;
    case 'b':
      break;
    default:
      break;
  }
  const foo = { bar: true };
  console.log('foo :', foo);
};

When running tsdx build, this is what I get:

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function wait() {
  return new Promise(function (resolve, reject) {
    setTimeout(function () {
      resolve();
    }, 1000);
  });
}

var test = function test() {
  try {
    return Promise.resolve(wait()).then(function () {
      switch ('') {
        case 'a':
          return;

        case 'b':
          return;

        default:
          return;
      }

      var foo = {
        bar: true
      };
      console.log('foo :', foo);
    });
  } catch (e) {
    return Promise.reject(e);
  }
};

exports.test = test;
//# sourceMappingURL=switch-test.cjs.development.js.map

As you can see, inside test, breaks inside then are transformed to returns, which is NOT correct. The console.log line would never be reached.

Expected behavior

breaks inside then should stay breaks.

Suggested solution(s)

No idea. The problem may come from babel or rollrup or one of their plugins. Any idea what may cause the problem?

Your environment

Software Version(s)
TSDX 0.12.3
TypeScript ^3.7.5
Browser
npm/Yarn yarn 1.19.1
Node 10.16.3
Operating System Linux, but same behavior on windows

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
agilgur5commented, Jul 19, 2020

Another comment on that issue revealing more broken code: https://github.com/rpetrich/babel-plugin-transform-async-to-promises/issues/49#issuecomment-656050437. And still no response / maintenance.

At this point, I think it would be optimal to switch to regenerator because at least the code it produces is actually correct and not broken in subtle, hard-to-detect ways. I’m also not sure really how much difference in weight it adds as a polyfill because babel-plugin-transform-async-to-promises also adds helpers, it’s not just a polyfill vs. no polyfill comparison.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I access previous promise results in a .then() chain?
In the end I want to return some composite value, and need to access multiple intermediate promise results. However the resolution values from ......
Read more >
Does the return statement break out of the loop? - Quora
If you're using return keyword inside a loop and the loop is inside a function, then the return statement will not only break...
Read more >
ES6 transpiled await breaks code if inside for/in loop #33812
Expected behavior: exampleA should behave like exampleB : wait for the promise to finish and then proceed into logging the following messages.
Read more >
How to Bundle JavaScript With Rollup — Step-by-Step Tutorial
By the end of this tutorial, we'll have Rollup configured to: combine our scripts,; remove unused code,; transpile it to work with older...
Read more >
Even with async/await, raw promises are still key to writing ...
In my experience, these transpilers work pretty well for simple functions, which await then return , perhaps with a try/catch block at most....
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