3

I have a nodeJS-Express-Typescript project where I want to use some native promises with async/await and also some default value for a function. This would be a simple example of what I can achieve:

sleep(ms: number) {
    return new Promise(resolve => setTimeout(resolve, 500));
}

async someFunction(param = "default") {
    doStuff(param);

    await sleep(500);

    doSomeMoreStuff();
}

The IDE warns me about this error:

$ tsc -p .

error TS2468: Cannot find global value 'Promise'.
spec/routes/users.spec.ts(508,23): error TS2705: An async function or method in ES5/ES3 requires the 'Promise' constructor.  Make sure you have a declaration for the 'Promise' constructor or include 'ES2015' in your `--lib` option.
src/utils/sleep.ts(10,20): error TS2693: 'Promise' only refers to a type, but is being used as a value here.

so I have to add es2015 as target in my tsconfig.json:

"target": "es2015"

But then, this error comes when executing the transpiled JS:

/../users-api/dist/src/repository/realm-helper.js:21
    static init(development = false) {
                            ^

SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (/../users-api/dist/src/routes/users.js:4:24)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

So that I have to change the target to "es5":

"target": "es5"

Which leads to a vicious circle.

I've tried changing the value of "target" and "module" and it always fails something.

Am I missing something here? In theory, typescript 2.2 supports both features so I don't get why I can't transpile.

tsconfig.json

{
    "compilerOptions": {
        "outDir": "./dist/",
        "rootDir": ".",
        "sourceMap": true,
        "module": "commonjs",
        "target": "es5"
    },
    "include": [
        "./src/**/*",
        "./spec/**/*"
    ]
}

typescript 2.4.1

node 4.4.7

1 Answer 1

4

Try Adding lib section with es2015.promise in tsconfig.json

    "lib": [
        "dom",
        "es5",
        "scripthost",
        "es2015.promise"
    ], 

You can see the full sample here: https://github.com/basarat/typescript-book/tree/master/code/async-await/es5

Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks for answering. After adding those, it throws me this error error TS2488: Type must have a '[Symbol.iterator]()' method that returns an iterator. in every line a have a for-of loop.
add "es2015" , then you will be adding full es2015 feature set
Nothing... same error Unexpected token = The code is here if you want to give a shot yourself :/ github.com/josemigallas/users-api/tree/async_await_es5

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.