简体   繁体   English

AngularJS是否承诺调度可与`async` /`await`一起使用?

[英]Does AngularJS promise scheduling work with `async`/`await`?

TypeScript constantly suggests that I change my AngularJS service code to async / await functions. TypeScript不断建议我将AngularJS服务代码更改为async / await功能。

TypeScript建议

My understanding is that using the await keyword is totally fine with third-party promises, since it is just syntax sugar for calling then . 我的理解是,使用await关键字对第三方promise完全没问题,因为这只是调用then语法糖。 However, I normally return Angular promises because they are necessary to play nicely with the digest cycle. 但是,我通常会返回Angular Promise,因为它们必须很好地与摘要循环配合使用。

TypeScript错误

This code gives me an error because async functions wrap their contents in an ES6 promise. 这段代码给我一个错误,因为async函数将它们的内容包装在ES6 Promise中。 Will this matter for Angular scheduling, given that the returned promise is still hooked up to an Angular-spawned promise? 鉴于返回的诺言仍与Angular产生的诺言挂钩,这对Angular调度有关系吗? Or should I submit an issue to TypeScript for suggesting async / await when functions do not explicitly return an ES6 promise? 还是应该在函数未明确返回ES6承诺时向TypeScript提出建议async / await的问题?

For anyone viewing this in the future. 对于将来查看此内容的任何人。 It does not play nicely. 它不能很好地播放。 async functions wrap their contents in a global ES6 promise, so if you await AngularJS promises within the changes will eventually hit, but scheduling is weird when you chain together $q promises and ES6 promises, so there will usually be an artificial delay before changes are reflected in the DOM. async函数将其内容包装在全局ES6 Promise中,因此,如果您等待AngularJS Promise最终会更改,但是将$q Promise和ES6 Promise链接在一起时,调度很奇怪,因此更改之前通常会有人为的延迟反映在DOM中。

On the other hand, Angular 2+ monkey-patches DOM event sources and promises, so async-await should work as expected with newer versions. 另一方面,Angular 2+猴子修补了DOM事件源和Promise,因此async-await应该可以在新版本中按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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