[英]How can I use await/async in TypeScript (angular2) when targeting Safari?
I have added some code similar to the following in my project: 我在我的项目中添加了一些类似于以下内容的代码:
async fun2(): Promise<string> {
let token: string = null;
await this.auth.getToken().then(newToken => {
token = newToken;
});
return token;
}
However, when it is compiling, I get the warning: 但是,在编译时,我收到警告:
Async functions are only available when targeting ECMAScript 2015 or higher.
When I try to run the project in Chrome it works fine, however in Safari I get an error on __awaiter
function. 当我尝试在Chrome中运行该项目时它工作正常,但在Safari中我在
__awaiter
函数上__awaiter
。
proj.prototype.fun2 = function () {
return __awaiter(this, void 0, Promise, function * () {
var token = null;
What do I need to do to use async/await in Safari with Typescript? 使用Typescript在Safari中使用async / await需要做什么?
In your tsconfig.json
, you need to set "target": "es6"
in the compilerOptions
section. 在
tsconfig.json
,需要在compilerOptions
部分设置"target": "es6"
。 Note that only Safari v10.0 is fully ES6 compliant, so you need to be on that version. 请注意,只有Safari v10.0完全符合ES6标准,因此您需要使用该版本。 Your code will likely not run in other browsers, as their
ES6/ES2015
support is not yet complete. 您的代码可能无法在其他浏览器中运行,因为他们的
ES6/ES2015
支持尚未完成。
Promise is part of ES2015, so nothing needs to be done if you switch to the es6
target. Promise是ES2015的一部分,因此如果切换到
es6
目标,则es6
。
UPDATE : As of TypeScript 2.1, you can use async/await
when targeting ES5. 更新 :从TypeScript 2.1开始,您可以在定位ES5时使用
async/await
。
1) Make sure you have TypeScript 2.1. 1)确保你有TypeScript 2.1。 Earlier versions don't fully support
async await
when targeting ES5. 在定位ES5时,早期版本不完全支持
async await
。
2) To make Promise
valid in ES5, follow the instructions of the accepted answer of this question: TypeScript Promise TS2304 TS2529 2)为了使
Promise
在ES5有效,遵循这个问题的答案接受的指令: 打字稿无极TS2304 TS2529
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.