要知道任何一个变量是angularjs承诺? [英] Any way to know if a variable is an angularjs promise?

查看:117
本文介绍了要知道任何一个变量是angularjs承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个指令,接受一个函数作为示波器参数(范围:{方法:'和; theFunction'} )。我需要知道,如果通过该方法返回的结果是一个棱角分明的承诺(如果有的东西就决议发生,否则它发生的时候了)。

I'm making a directive that takes a function as a scope parameter (scope: { method:'&theFunction' }). I need to know if the result returned by that method is an angular promise (if yes something will happen on resolution, otherwise it happens right away).

现在我如果 foo.then 测试存在,但我不知道是否有更好的方式来做到这一点。

For now I'm testing if foo.then exists but I was wondering if there was a better way to do it.

推荐答案

您可以使用 $ q.when 来包装对象的承诺(无论是与否)。然后,你可以肯定,你总是处理一个承诺。这应该简化code,它再处理结果。

You can use $q.when to wrap the object as a promise (whether it is or not). Then, you can be sure that you are always dealing with a promise. This should simplify the code that then handles the result.

的文档 $ q.when 用$ Q 。

这篇关于要知道任何一个变量是angularjs承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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