breaks are transpiled to return inside then
See original GitHub issueCurrent 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
, break
s inside then
are transformed to return
s, which is NOT correct. The console.log
line would never be reached.
Expected behavior
break
s inside then
should stay break
s.
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:
- Created 4 years ago
- Reactions:3
- Comments:16 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.async-to-promises
has been replaced withbabel-plugin-polyfill-regenerator
in #795 and will be released in v0.14.0 soon. It will only pure polyfill generators fortargets
that need a polyfill according to yourbrowserslistrc
orpreset-env
targets