Bug when mixing async/await and spreading on function's arguments
See original GitHub issueBug Report
- I would like to work on a fix!
Why not if I can have some hints on where to look for 🤔
Current Behavior A clear and concise description of the behavior.
It seems that the code produced when transpiling (await p).print(...args) using @babel/preset-env with its default configuration is not doing the same as the original code. The print function is called with an undefined context while I expected it to be called with the context of the result of await p.
By the way a quick fix (in my case) was to extract await p into a variable first and call print on this variable right after - see the linked repo. The bug only occur when I have both await and ...args on the same statement.
Input Code
- REPL or Repo link if applicable: https://github.com/dubzzz/babel-async-bug
async function code(p, ...args) {
// Original code: Succeeds
// Transpiled code: Fails with "TypeError: Cannot read property 'x' of undefined"
console.log((await p).print(...args));
}
// Example
const instance = {
x: 0,
y: 1,
print() {
return `${this.x} ${this.y}`;
}
};
const promiseInstance = Promise.resolve(instance);
code(promiseInstance);
Expected behavior/code A clear and concise description of what you expected to happen (or code).
I expect the transpile version of the code to behave the same way as the original source code.
Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)
- Filename:
.babelrc
{
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"regenerator": true
}
]
],
"presets": ["@babel/preset-env"]
}
Environment
System:
OS: Windows 10 10.0.19041
Binaries:
Node: 12.11.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.19.1 - ~\AppData\Roaming\npm\yarn.CMD
npm: 6.11.3 - C:\Program Files\nodejs\npm.CMD
- Babel version(s):
- “@babel/cli”: “7.8.4”,
- “@babel/core”: “7.8.7”,
- “@babel/plugin-transform-runtime”: “7.8.3”,
- “@babel/preset-env”: “7.8.7”
- How you are using Babel:
cli
Possible Solution
Additional context/Screenshots Add any other context about the problem here. If applicable, add screenshots to help explain.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:5 (3 by maintainers)

Top Related StackOverflow Question
Thanks for the repository! It makes it way easier to debug this bug.
I managed to make a simpler reproduction:
PS. I noticed that you are using
.babelrc. It works, but probably what you want isbabel.config.js/babel.config.json!Fixed by https://github.com/facebook/regenerator/pull/386