I have transpiled a javascript code from es7 to es6 since I need to use
Node.js 6.9.5
but I keep getting this error when transpiling:
Unexpected Identifier keyValues[key] = yield DbStorage.get(key);
my code looks like this:
getMany: function (keys) {
return new Promise((resolve, reject) => {
let keyValues = {};
for(let key of keys){
keyValues[key] = await DbStorage.get(key);
}
resolve(keyValues);
});
},
and the transpiled code looks like this:
getMany: function (keys) {
return new Promise((resolve, reject) => {
let keyValues = {};
for (let key of keys) {
keyValues[key] = yield DbStorage.get(key);
}
resolve(keyValues);
});
},
I am using typescript to transpile my tsconfig.json looks like this:
{
"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" ]
}
so what's wrong with this ?
You're using await
in a non-async
function, which is incorrect. getMany
should be async
, which amongst other things means you don't need new Promise
:
getMany: async function (keys) {
// ^^^^^
let keyValues = {};
for (let key of keys){
keyValues[key] = await DbStorage.get(key);
}
return keyValues;
},
It's very strange of the TypeScript compiler to turn that erroneous await
into an erroneous yield
, but it could be an edge-case bug in the TypeScript compiler. If you fix the usage, hopefully it will transpile correctly.