如何返回完全解决的承诺? [英] How to return fully resolved promise?

查看:27
本文介绍了如何返回完全解决的承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 $q,编写一些测试,尝试存根承诺等,我想知道是否有一种方法可以返回完全解析的承诺,就像使用 whenjs, when("stuff to return),就等于这个

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

澄清:我知道此代码有效,但我想在不编写此函数的情况下执行此操作.我想做这样的事情 $q(expectedresponse) 并获得相当于上面的代码.这就是我所追求的.就像使用 whenjs 一样,您可以编写 when(stuffToResolve) 并且它会返回一个完全解析的 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.

推荐答案

在发布说明后,我重新阅读了文档,发现了 - when() 方法.所以我可以使用 $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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆