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?
{
"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"
]
}
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.
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.
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.
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 .
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)
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.
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.
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).--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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With