我试图获得支持await
令牌的最新babel / babylon软件包,但我仍然看到相同的错误。
错误的原因可能是await
位于map()
函数内,并且它超出了原始函数范围。
配置中缺少什么?
index.js:
async getLocations(locations) {
var annotations = locations.map(function(location) {
try {
let res = await Geocoder.geocodeAddress(location.address);
console.log(res);
console.log(res[0].position);
}
catch(err) {
console.log(err);
}
return (
{
latitude: res[0].position.lat,
longitude: res[0].position.lng
}
);
});
}
控制台:
transformed 1/2 (50%)[node-haste] Encountered an error while persisting cache:
> SyntaxError: /Users/user/Developer/ReactProject/index.js: Unexpected token (122:25)
> 120 | var annotations = inspections.map(function(inspection) {
> 121 | try {
> > 122 | let res = await Geocoder.geocodeAddress(address);
> | ^
> 123 |
> 124 | console.log(res);
> 125 | console.log(res[0].position);
> at Parser.pp.raise (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/location.js:22:13)
> at Parser.pp.unexpected (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/util.js:89:8)
> at Parser.pp.parseAwait (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/expression.js:1082:10)
> at Parser.pp.parseExprAtom (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/expression.js:423:25)
> at Parser.pp.parseExprSubscripts (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/expression.js:277:19)
> at Parser.pp.parseMaybeUnary (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/expression.js:257:19)
> at Parser.pp.parseExprOps (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/expression.js:188:19)
> at Parser.pp.parseMaybeConditional (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/expression.js:165:19)
> at Parser.pp.parseMaybeAssign (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/expression.js:128:19)
> at Parser.pp.parseVar (/Users/user/Developer/ReactProject/node_modules/babylon/lib/parser/statement.js:601:24)
的package.json
"dependencies": {
"babel": "^6.5.2",
"babel-core": "^6.1.2",
"babel-plugin-transform-runtime": "^6.1.2",
"babel-polyfill": "^6.9.1",
"babel-preset-es2015": "^6.1.2",
"babel-preset-stage-0": "^6.1.2",
"babel-preset-stage-3": "^6.11.0",
"babylon": "^6.8.2",
"react": "^15.1.0",
"react-native": "0.27.2",
},
.babelrc:
{
"presets": [ "es2015", "stage-3" ]
}
答案 0 :(得分:1)
使用await
的函数必须声明为async
:
inspections.map(async function(inspection) {
答案 1 :(得分:0)
试试这个,
{
"plugins": ["transform-async-to-generator"]
}
https://babeljs.io/docs/plugins/transform-async-to-generator/