M
M
Michael2017-01-25 13:34:42
Node.js
Michael, 2017-01-25 13:34:42

Why can't use async/await in TypeScript?

I write in typescript using the koa framework. tsconfig.json is like this:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es6",
        "noImplicitAny": false,
        "sourceMap": false,
        "typeRoots": [
            "../node_modules/@types"
        ]
    }
}

The code is like this:
const koa = require('koa'),
      app = new koa();

app.use(async (ctx, next) => {
      const start: any = new Date();
      await next();
      const ms: any = 10 - start;
      console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

app.use(ctx => {
   ctx.body = 'kek';
   console.log('start');
});
app.listen(3000);

And in the console the error is:
error TS1311: Async functions are only available when targeting ECMAScript 2015 or higher.

Walked decisions. Everywhere they write that you just need to compile in ES2015. But I do it already. So what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ruslan Lopatin, 2017-01-25
@mak_ufo

TypeScript has supported async/await since version 2.1. And even when it compiles to ES3. Most likely, you do not have 2.1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question