Babel使用webpack在以下代码片段和配置中正确地解释了所有内容:
async function myMethod(i) {
let j = i * i;
return j;
}
let x = myMethod(2);
console.log(myMethod)
console.log(x);
let y = await myMethod(2);
结果是:
console.log(myMethod) // returns a function.
console.log(x) // returns a promise
现在奇怪的是,在最后一行我得到一个“意外的令牌”错误形式babel。怎么会这样?
的package.json:
"devDependencies": {
"babel": "^6.5.2",
"babel-loader": "^6.2.4",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-0": "^6.5.0",
"react": "^0.14.8"
}
Webpack.config:
var path = require('path');
var webpack = require('webpack');
module.exports = {
context: path.join(__dirname),
entry: ['babel-polyfill', './App/main.js'],
[..]
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: ['es2015', 'stage-0', 'react'],
},
},
],
},
[..]
resolve: {
extensions: ['', '.js', '.jsx'],
}
}
答案 0 :(得分:3)
据我所知,要使用await
,必须将其包含在async
函数中。
所以你可以改变:
let y = await myMethod(2);
是:
async function main() {
let y = await myMethod(2);
}
main();
它应该有效。