Last active
January 25, 2016 18:37
-
-
Save djanowski/3de609926981865bb3aa to your computer and use it in GitHub Desktop.
Babel bug with `this` inside async arrow function - npm i && npm test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
babel-preset-stage-2 pulls in the newest version of babel-plugin-transform-async-to-generator, | |
however the async arrow function still has `this` = `undefined`. | |
If I manually install babel-plugin-transform-async-to-generator and add it to the list of plugins | |
in .babelrc, everything works as expected. | |
Steps to reproduce: | |
$ npm install && npm test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"presets": ["es2015-node4", "stage-2"], | |
"plugins": ["transform-es2015-arrow-functions"] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "babel-preset-async-arrow", | |
"version": "1.0.0", | |
"scripts": { | |
"test": "babel-node test.js" | |
}, | |
"engines": { | |
"node": "4.2.3" | |
}, | |
"dependencies": { | |
"babel-plugin-transform-async-to-generator": "^6.4.6", | |
"babel-plugin-transform-es2015-arrow-functions": "^6.3.13", | |
"babel-preset-es2015-node4": "^2.0.2", | |
"babel-preset-stage-2": "^6.3.13", | |
"babel-register": "^6.3.13" | |
}, | |
"devDependencies": { | |
"babel-eslint": "^5.0.0-beta6", | |
"babel-plugin-transform-runtime": "^6.3.13", | |
"babel-preset-es2015": "^6.3.13", | |
"babel-runtime": "^6.3.13" | |
}, | |
"private": true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
(function() { | |
console.log(`It works if next 3 lines are`, this); | |
[1,2,3].map(async (foo)=> { console.log(this) }); | |
}).bind({ x: 1 })(); | |
// Output: | |
// | |
// It works if next 3 lines are { x: 1 } | |
// undefined | |
// undefined | |
// undefined | |
// | |
// Expected output: | |
// It works if next 3 lines are { x: 1 } | |
// { x: 1 } | |
// { x: 1 } | |
// { x: 1 } | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment