简体   繁体   中英

Use async / await in TypeScript targeting ES5

I have a typescript project that's targeting ES5 and I am trying to determine whether or not it will be possible to use async / await.

Syntatically, the Typescript compiler will transpile the code just fine however I understand that I must provide a polyfill for 'Promise' since the object does not exist in ES5.

I am trying to use the npm 'promise' package for this, but I am encountering the following error when I include it in the same file where I have an 'async / await' function:

Duplicate identifier 'Promise'. Compiler reserves name 'Promise' in top level scope of a module containing async functions.

Therefore, I'm not sure how I can provide the 'Promise' functionality so that the code does not error at runtime.

Any workarounds for this? Thanks!

If you're on node.js, I recommend using ts-node. If you're developing with React, try scaffolding a starter with Create-React-App. Not sure about Angular. Instead of trying to put together your own transpiler config, these are better options that work out of the box.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM