0

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 ?

1 Answers1

0

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.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • ah thank you T.J. Crowder, for the good answer yes it did got transpiled very good. Do you suggest using another transpiler ? –  Aug 21 '17 at 07:05
  • @gentos It isn't a bug in TypeScript. TypeScript has two "phases", the compile phase (which is the type and syntax checking) and the transpile phase (which is just emitting target code). You somehow skipped the compile stage and completely ignored the errors. – Madara's Ghost Aug 21 '17 at 07:07
  • @gentos It does, how do you call TypeScript? A loader? `tsc`? Directly from Node? – Madara's Ghost Aug 21 '17 at 07:20
  • @gentos: Not if you're using TypeScript for its type features, no. But do pay attention to the error messages. TypeScript's compiler emits code even when there are errors (at least in some cases, I'm not a TS expert), so it's important to check those errors. – T.J. Crowder Aug 21 '17 at 07:20
  • @MadaraUchiha no I am calling through the terminal via: tsc and than doing a watch via: tsc -w –  Aug 21 '17 at 07:20