Asynchronous Generators Missing in Babel 6
In Babel 6, you may encounter the error "regeneratorRuntime is not defined" while attempting to implement asynchronous generators. This occurs because the regenerator runtime, which forms the foundational basis for asynchronous functions, needs to be included alongside Babel.
Solution: Include Babel-polyfill
To resolve this error, you'll need to install babel-polyfill, which provides support for asynchronous functions.
npm i -D babel-core babel-polyfill babel-preset-es2015 babel-preset-stage-0 babel-loader
Updated Configuration
After installing babel-polyfill, update your package.json's "devDependencies" section with the new package.
package.json
"devDependencies": { "babel-core": "^6.0.20", "babel-polyfill": "^6.0.16", "babel-preset-es2015": "^6.0.15", "babel-preset-stage-0": "^6.0.15" }
Incorporating the Polyfill
In your startup file, require both babel-core/register and babel-polyfill.
startup file
require("babel-core/register"); require("babel-polyfill");
For webpack users, remember to place 'babel-polyfill' as the first entry in your entry array.
webpack configuration
module.exports = { entry: ['babel-polyfill', './test.js'], output: { filename: 'bundle.js' }, module: { loaders: [ { test: /\.jsx?$/, loader: 'babel', } ] } };
In the case of testing using Mocha, employ the following command:
mocha --compilers js:babel-core/register --require babel-polyfill
With these adjustments, asynchronous generators will now be accessible in your Babel 6 environment, allowing you to seamlessly write and utilize async/await syntax.
The above is the detailed content of Why is \'regeneratorRuntime is not defined\' in Babel 6 when using asynchronous generators, and how can I fix it?. For more information, please follow other related articles on the PHP Chinese website!