Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot transpile TypeScript containing async await

When trying to transpile the following TypeScript code containing async and await keywords

async function foo() {
    await bar();
}

I get the following error

src/aa.ts(1,7): error TS1005: ';' expected.
src/aa.ts(2,11): error TS1005: ';' expected.

The result is a .js file with this content

async;
function foo() {
    await;
    bar();
}

I'm using these tsc options: -t es6 -m commonjs, following instructions on this MSDN blog. I have TypeScript 1.8.9 installed.

Any ideas?

like image 809
Perspectivus Avatar asked Apr 05 '16 10:04

Perspectivus


People also ask

Can we use async await in TypeScript?

Async - Await has been supported by TypeScript since version 1.7. 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 does await () do in Java?

The await() method of the Java CyclicBarrier class is used to make the current thread waiting until all the parties have invoked await() method on this barrier or the specified waiting time elapses.

What is equivalent async await in Java?

Java has unfortunately no equivalent of async/await. The closest you can get is probably with ListenableFuture from Guava and listener chaining, but it would be still very cumbersome to write for cases involving multiple asynchronous calls, as the nesting level would very quickly grow.

Do we have async await in Java?

Electronic Arts brought the async-await feature from . NET to the Java ecosystem through the ea-async library. This library allows writing asynchronous (non-blocking) code sequentially. Therefore, it makes asynchronous programming easier and scales naturally.


2 Answers

For some reason the TypeScript compiler did not recognize the async and await keywords. This happened even though the TypeScript compiler version was of the correct version.

What I did to solve this is uninstall tsc and install typescript globally:

npm uninstall --global tsc
npm install --global typescript
like image 104
Perspectivus Avatar answered Sep 25 '22 06:09

Perspectivus


I encountered a similar issue with an asynchronous arrow function:

async resource_type => some_value
// error TS1005: ',' expected.

Typescript was happy once I wrapped my function parameter in parenthesis:

async (resource_type) => some_value

like image 27
spiffytech Avatar answered Sep 22 '22 06:09

spiffytech