将 es7 转译为 es6 错误意外标识符
Transpiling es7 to es6 error unexpected identifier
我已经将 javascript 代码从 es7 转译为 es6,因为我需要使用
Node.js 6.9.5
但是我在转译时一直收到这个错误:
意外标识符键值[key] = yield DbStorage.get(key);
我的代码如下所示:
getMany: function (keys) {
return new Promise((resolve, reject) => {
let keyValues = {};
for(let key of keys){
keyValues[key] = await DbStorage.get(key);
}
resolve(keyValues);
});
},
转译后的代码如下所示:
getMany: function (keys) {
return new Promise((resolve, reject) => {
let keyValues = {};
for (let key of keys) {
keyValues[key] = yield DbStorage.get(key);
}
resolve(keyValues);
});
},
我正在使用打字稿来转译我的 tsconfig.json 看起来像这样:
{
"allowJs" : true,
"compilerOptions": {
"target": "es6",
"sourceMap": true,
"removeComments": false,
"listFiles" : false,
"diagnostics" : false,
"outDir" : "build",
"allowJs" : true,
"inlineSourceMap" : false
},
"include" : ["collaboration/*"],
"exclude": [ "build", "node_modules" ]
}
这有什么问题吗?
您在非 async
函数中使用 await
,这是不正确的。 getMany
应该是 async
,这意味着您不需要 new Promise
:
getMany: async function (keys) {
// ^^^^^
let keyValues = {};
for (let key of keys){
keyValues[key] = await DbStorage.get(key);
}
return keyValues;
},
TypeScript 编译器将错误的 await
转换为错误的 yield
非常奇怪,但这可能是 TypeScript 编译器中的一个边缘案例错误。如果您修复了用法,希望它能正确转译。
我已经将 javascript 代码从 es7 转译为 es6,因为我需要使用
Node.js 6.9.5
但是我在转译时一直收到这个错误:
意外标识符键值[key] = yield DbStorage.get(key);
我的代码如下所示:
getMany: function (keys) {
return new Promise((resolve, reject) => {
let keyValues = {};
for(let key of keys){
keyValues[key] = await DbStorage.get(key);
}
resolve(keyValues);
});
},
转译后的代码如下所示:
getMany: function (keys) {
return new Promise((resolve, reject) => {
let keyValues = {};
for (let key of keys) {
keyValues[key] = yield DbStorage.get(key);
}
resolve(keyValues);
});
},
我正在使用打字稿来转译我的 tsconfig.json 看起来像这样:
{
"allowJs" : true,
"compilerOptions": {
"target": "es6",
"sourceMap": true,
"removeComments": false,
"listFiles" : false,
"diagnostics" : false,
"outDir" : "build",
"allowJs" : true,
"inlineSourceMap" : false
},
"include" : ["collaboration/*"],
"exclude": [ "build", "node_modules" ]
}
这有什么问题吗?
您在非 async
函数中使用 await
,这是不正确的。 getMany
应该是 async
,这意味着您不需要 new Promise
:
getMany: async function (keys) {
// ^^^^^
let keyValues = {};
for (let key of keys){
keyValues[key] = await DbStorage.get(key);
}
return keyValues;
},
TypeScript 编译器将错误的 await
转换为错误的 yield
非常奇怪,但这可能是 TypeScript 编译器中的一个边缘案例错误。如果您修复了用法,希望它能正确转译。