任何方式来知道如果一个variables是一个angularjs的承诺?

我正在做一个指令,把一个函数作为一个范围参数( scope: { method:'&theFunction' } )。 我需要知道,如果这个方法返回的结果是一个angular度承诺(如果是的话会发生在解决scheme,否则它会立即发生)。

现在我正在testing如果foo.then存在,但我想知道是否有更好的方法来做到这一点。

您可以使用$q.when将对象封装为承诺(无论是否)。 那么,你可以肯定你总是在处理一个承诺。 这应该简化处理结果的代码。

$q.when文档在$ q中 。

when() Davin提到的when() Angular是一个很好的select。

如果这不符合您的需要,那么Angular的内部检查方法 (它在内部使用这个方法)非常接近您正在做的事情:

 var ref = function(value) { if (value && isFunction(value.then)) { // Then this is promise } 

@kayakDave,感谢您的指导,以正确的地方。

angular$ q

 when(value, [successCallback], [errorCallback], [progressCallback]); Wraps an object that might be a value or a (3rd party) then-able promise into a $q promise. This is useful when you are dealing with an object that might or might not be a promise, or if the promise comes from a source that can't be trusted. 
 $q.when(value).then(function (data) { //this helps me to bind data from $resource or $http or object } 

检查这个小提琴