Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__awaiter is not defined when using async/await in Typescript

I have the following code snippet in Typescript:

nsp.on('connection', async function (socket) {
    await this.emitInitialPackage(nsp, currentLine, currentCell);
}

emitInitialPackage(nsp: any, name: string, cell: any) {
    return db.Line.find({
        where: {
            name: name,
            CellId: cell
        }
    }).then(results => {
        nsp.emit('value', results);
    }).catch(err => console.log(err));
}

However, when this is compiled (v2.2.1) and run, I get the following error:

Uncaught ReferenceError: __awaiter is not defined

What does this mean and how do I get the expected functionality?

Update:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "noEmitHelpers": true,
    "strictNullChecks": false,
    "lib": [
      "dom",
      "es2015.promise", 
      "es5"
    ],
    "types": [
      "node", 
      "express"
    ]
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}
like image 664
George Edwards Avatar asked Feb 23 '17 12:02

George Edwards


People also ask

How do I use async await in TypeScript?

async / await support in ES6 targets (Node v4+) Asynchronous functions are prefixed with the async keyword; await suspends the execution until an asynchronous function return promise is fulfilled and unwraps the value from the Promise returned.

What is __ Awaiter?

The__awaiter function wraps the function body, including the yield statement, in a Promise that executes the function as a generator. Trying it out with Node.js. Starting with nightly builds, TypeScript 1.7 now supports async/await for ES6 targets.

What is the return type of async await TypeScript?

An async/await will always return a Promise . Even if you omit the Promise keyword, the compiler will wrap the function in an immediately resolved Promise . This enables you to treat the return value of an async function as a Promise , which is quite useful when you need to resolve numerous asynchronous functions.

How do I enable top level await in TypeScript?

To use top-level await in TypeScript, you have to set the target compiler option to es2017 or higher. The module option has to be set to esnext or system .


2 Answers

The accepted answer didn't work in my case, but I found that my tsconfig.json was targeting es6 ("target":"es6").

What this means is that TypeScript transpiles to code that uses the __awaiter util because async await was not included in the spec until ES2017.

I fixed this by changing my target to ESNext (or anything ES2017 and above)

like image 194
Jamie Avatar answered Oct 12 '22 11:10

Jamie


When you use some functionalities from future version of JavaScript (ES6 and beyond) like in your case async/await, TypeScript generates helper functions. These helper functions are used to provide the new functionalities as ES5 code, thus it can be run in a web browser.

Your problem:

In your tsconfig.json you set the noEmitHelpers value to true. By doing that you tell the TypeScript compiler that you will provide these helper functions yourself.

How to fix it:

  • You can set the noEmitHelpers value to false in your tsconfig.json, thus the TypeScript compiler will emit the helper functions when needed. One drawback of this method is that if you use for example async/await in 2 different files, the helper functions will be emitted 2 times (one per file).
  • The other solution is to set the --importHelpers flag when you use tsc. It will tell the TypeScript compiler to include the helper functions only once. Please note that if you use this solution you have to install the tslib package.

In your case: tsc --importHelpers -w

like image 38
Romain Avatar answered Oct 12 '22 13:10

Romain