有什么方法可以知道变量是否是 angularjs 承诺? [英] Any way to know if a variable is an angularjs promise?

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

问题描述

我正在制作一个将函数作为作用域参数的指令(scope: { method:'&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 将对象包装为承诺(无论是否).然后,您可以确定您总是在处理一个承诺.这应该会简化随后处理结果的代码.

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天全站免登陆