解决的承诺没有消化周期 [英] Resolving promises without a digest cycle
问题描述
AngularJS的承诺似乎绑在消化周期,如,成功/错误回调不叫,直到消化周期运行。这意味着任何使用承诺,如 $ HTTP
或手动创建的承诺,还需要为了得到回调运行到触发消化周期。
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.
是否有可能使用的承诺在棱角分明,没有消化周期正在运行的呢?我知道你可以使用 $ applyAsync
,其中调度消化周期晚了一点,但我希望不是在所有运行的消化周期,仍然有然后
回调运行。
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.
推荐答案
没有,它是目前不可能的。每当然后
处理程序运行它的时间表通过 $ 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.
的例外是 $超时
接受一个额外的参数不运行摘要。在另一方面 - 即解决在相同摘要
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.
您的选项有:
- 使用XMLHtt prequqest直接,严重 - 这不是很辛苦。这个最大的缺点是,它不会尊重拦截器和其他的$ HTTP挂钩(如模拟后端)。
- 装饰 $ Q
不通过 $ evalAsync
计划或添加一个 .thenNoDigest
法的承诺原型,通过的setTimeout
。
- 使用用户级库的承诺,而不是$ Q $为http或超过XHR。
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.
这篇关于解决的承诺没有消化周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!