简体   繁体   English

在定位Safari时如何在TypeScript(angular2)中使用await / async?

[英]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.

相关问题 在面向 ES5 的 TypeScript 中使用 async / await - Use async / await in TypeScript targeting ES5 如何在.ts文件中使用async / await(我收到错误消息:“异步功能仅在针对ecmascript 6或更高版本时可用”) - How to use async/await in .ts file ( I get error : “ async functions are only available when targeting ecmascript 6 or higher” ) 如何在打字稿中使用异步/等待 - How to use async/await in typescript 如何使用 Typescript 在 Vue 3.0 setup() 函数中使用 async/await - How can I use async/await in the Vue 3.0 setup() function using Typescript 定位-es5时,如何摆脱“找不到地图,Promise ...”上的Angular2 / TypeScript错误? - How to get rid of Angular2 / TypeScript errors on Cannot find map, Promise … when targeting -es5? 如何使用 TypeScript 运行顶级异步/等待 function? - How can I run a top level async / await function with TypeScript? 定位es5时如何将Promises与TypeScript一起使用? - How do I use Promises with TypeScript when targeting es5? 如何使用顶级异步等待 typescript? - how to use top level async await with typescript? 我可以在Angular2 Typescript项目中使用.dll吗 - Can I use .dll in Angular2 Typescript project 如何在Typeular的Angular2组件中使用process.cwd()? - How can I use process.cwd() within Angular2 Component with TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM