How to return fully resolved promise?

杀马特。学长 韩版系。学妹 提交于 2019-12-02 22:01:08

问题


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;
    }

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.


回答1:


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.



来源:https://stackoverflow.com/questions/20613980/how-to-return-fully-resolved-promise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!