如何返回完全解决的承诺? [英] How to return fully resolved promise?
问题描述
我试图玩弄$ Q,写一些测试,尝试存根承诺等,我想知道是否有返回完全解决的承诺就像人能whenjs做到这一点的一种方式,当(东西返回),一些将等于这个
I am trying to play around with $q, write some tests, try to stub promises etc. and I wondered if there is a way to return fully resolved promise like one can do it with whenjs, when("stuff to return), something that would be equal to this
function fullyResolvedPromise(expectedResponse) {
var dfd = $q.defer();
dfd.resolve(expectedResponse);
$rootScope.$apply();
return dfd.promise;
}
澄清:我知道这code的作品,但我想这样做,而无需编写此功能。我想要做这样的事情$ Q(expectedresponse),并获得等同于上述code。这就是我所追求的。就像使用whenjs你可以写的时候(stuffToResolve),它会返回你一个完全解决的承诺。
Clarification: I know this code works, but I want to do it without writing this function. I want to do something like this $q(expectedresponse) and get equivalent to above code. That is what I am after. Just like with whenjs you can write when(stuffToResolve) and it would return you a fully resolved promise.
推荐答案
发布澄清后,我重新阅读文档,它就在那里 - 当()方法。
因此,我可以用$ q.when(stuffToResolve),这将是等于调用上述功能。
After posting a clarification I re-read the documentation and there it is - when() method. So I can use $q.when(stuffToResolve) and it would be equal to calling the above function.
这篇关于如何返回完全解决的承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!