Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@djanowski
Last active January 25, 2016 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djanowski/3de609926981865bb3aa to your computer and use it in GitHub Desktop.
Save djanowski/3de609926981865bb3aa to your computer and use it in GitHub Desktop.
Babel bug with `this` inside async arrow function - npm i && npm test
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
{
"presets": ["es2015-node4", "stage-2"],
"plugins": ["transform-es2015-arrow-functions"]
}
{
"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
}
'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