简体   繁体   English

在没有消化周期的情况下解决承诺

[英]Resolving promises without a digest cycle

AngularJS promises seem to be tied to a digest cycle, as in, the success/error callbacks are not called until a digest cycle is run. AngularJS承诺似乎与摘要周期联系在一起,因为在运行摘要周期之前不会调用成功/错误回调。 This means that anything that uses promises, such as $http or manually created promises, also need to trigger a digest cycle in order to get the callbacks to run. 这意味着任何使用promises的东西,比如$http或手动创建的promises,都需要触发一个摘要周期才能让回调运行。

Is it possible to use promises in Angular, without the digest cycle being run at all? 是否可以在Angular中使用promises,而根本不运行摘要周期? I realise you can use $applyAsync , which schedules the digest cycle for a bit later, but I'm looking to not run the digest cycle at all, and still have the then callbacks run. 我意识到你可以使用$applyAsync$applyAsync摘要周期进行调度,但我希望根本不运行摘要周期,并且仍然会运行then回调。

Essentially I'm trying to work out how to squeeze as much performance as possible from an app that would use a fair bit of asynchronous behaviour that would need promises resolved but not necessarily the digest cycle run. 本质上我正在尝试研究如何从应用程序中尽可能多地挤出性能,该应用程序将使用需要解决的承诺而不一定是摘要周期运行的相当多的异步行为。

No, it is currently not possible. 不,目前不可能。 Whenever a then handler runs it schedules the callback via $evalAsync which schedules a digest if one is not already scheduled. 每当一个then处理程序运行时,它会通过$evalAsync安排回调,如果尚未安排一个摘要,它将安排一个摘要。

The exception to this is $timeout that accepts an extra argument to not run a digest. 例外情况是$timeout接受额外的参数以不运行摘要。 On the other hand - multiple promises that resolve in the same turn run on the same digest. 另一方面 - 在同一个摘要中运行的多个promise将在同一个摘要中运行。

Your options are: - Use XMLHttpRequqest directly, seriously - it's not very hard. 您的选择是: - 直接使用XMLHttpRequqest,认真 - 这不是很难。 The biggest downside to this is that it will not respect interceptors and other $http hooks (like the mock backend). 最大的缺点是它不会尊重拦截器和其他$ http钩子(比如模拟后端)。 - Decorate $q to not schedule via $evalAsync or add a .thenNoDigest method to the promise prototype that schedules via setTimeout . - 装饰$q不通过$evalAsync计划或将.thenNoDigest方法添加到通过setTimeout计划的promise原型。 - Use a userland promise library instead of $q for $http or over XHR. - 对于$ http或XHR,使用userland promise库而不是$ q。

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

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