在没有摘要循环的情况下解决

在没有摘要循环的情况下解决

本文介绍了在没有摘要循环的情况下解决 promise的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AngularJS 承诺似乎与摘要循环相关联,因为在运行摘要循环之前不会调用成功/错误回调.这意味着任何使用 Promise 的东西,例如 $http 或手动创建的 Promise,也需要触发摘要循环才能运行回调.

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. 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.

是否可以在 Angular 中使用 promise,而根本不运行摘要循环?我意识到您可以使用 $applyAsync,它会在稍后安排摘要周期,但我希望根本不运行摘要周期,并且仍然拥有 then 回调运行.

Is it possible to use promises in Angular, without the digest cycle being run at all? 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.

从本质上讲,我试图找出如何从一个应用程序中尽可能多地榨取性能,该应用程序将使用相当多的异步行为,需要解决承诺,但不一定要运行摘要周期.

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.

推荐答案

不,目前不可能.每当 then 处理程序运行时,它都会通过 $evalAsync 安排回调,如果尚未安排摘要,则会安排摘要.

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.

例外情况是 $timeout,它接受一个额外的参数来不运行摘要.另一方面 - 在同一轮中解决的多个承诺在同一个摘要上运行.

The exception to this is $timeout that accepts an extra argument to not run a digest. On the other hand - multiple promises that resolve in the same turn run on the same digest.

您的选择是:- 认真地直接使用 XMLHttpRequqest - 这不是很难.最大的缺点是它不会尊重拦截器和其他 $http 钩子(如模拟后端).- 将 $q 修饰为不通过 $evalAsync 进行调度或将 .thenNoDigest 方法添加到通过 setTimeout.- 在 $http 或 XHR 上使用用户空间承诺库而不是 $q.

Your options are: - Use XMLHttpRequqest directly, seriously - it's not very hard. The biggest downside to this is that it will not respect interceptors and other $http hooks (like the mock backend). - Decorate $q to not schedule via $evalAsync or add a .thenNoDigest method to the promise prototype that schedules via setTimeout. - Use a userland promise library instead of $q for $http or over XHR.

这篇关于在没有摘要循环的情况下解决 promise的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 23:09