Async functions are only available when targeting ECMAScript 2015 or
higher.
我认为针对es6意味着编译打字稿的输出javascript将是es6,而es6在大多数浏览器上都不起作用了吗?
这是我的tsconfig.json:
{ "compilerOptions": { "target": "es5","module": "system","moduleResolution": "node","lib": ["es2015","dom"],"sourceMap": true,"emitDecoratorMetadata": true,"experimentalDecorators": true,"removeComments": false,"noImplicitAny": true,"outDir":"js/app/","typeRoots": [ "./node_modules/@types" ] },"exclude": [ "node_modules","app/test" ] }
我想知道lib字段是否可以使它工作,因为它说“es2015”但它没有.
我也在想也许core-js shim可以使异步可用,但我只在我的index.html中包含了这样的垫片:
< script src =“node_modules / core-js / client / shim.min.js”>< / script>
所以我不认为它在编译时使异步可用.有没有更好的方法来导入core-js?
如何使异步可用,但也没有我的代码无法在大多数浏览器上运行?
正如@loganfsmyth对此答案的评论所述,
Just keep in mind that “most major browsers” could still only cover a
tiny percentage of your user base.
ES6 The ECMAScript 2015 standard,also known as ES6,is completely
supported,bringing this major JavaScript evolution to Safari on macOS
and iOS.
你将不得不改变,
"target": "es5",
至
"target": "es6",
TypeScript 1.7 async/await support in ES6 targets (Node v4+) TypeScript now supports asynchronous functions for engines that have native support for ES6 generators,e.g. Node v4 and above. 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.