2015-01-20 76 views
2

我有一个函数可能会返回常规对象或http请求对象。如何在我的情况下返回一个对象?

我有类似

var t = function() { 
    var obj 
    var test; 
    //code to determine test value 

//return object depends on test value, 
//if test is undefined, return regular obj, 
//if not make a http request. 
    if (!test){ 
      return obj; 
    } 
    return getObj(url) 
     .then(function(obj){ 
      return obj 
     }) 
} 

var getObj = function() { 
    return $http.get(url);  
} 

var open = function() { 
    //this won't work for regular object, it has to be http object 
    return t() 
     .then(function(obj) { 
      return obj; 
     }) 
} 

var obj = open(); 

如何检查是否返回的对象是通过HTTP请求或只是一个普通的对象呢?

感谢您的帮助!

回答

3

如果我正确的理解问题是与t返回的对象是一个承诺或不启用c海宁。您可以始终使用$q.when(obj)包装对象,这样可以确保返回的对象始终是承诺,并且可以链接在一起。您需要确保按照您的方式$http注入$q。或者用var obj = $q.when(value)return obj包装测试值本身。

var t = function() { 
    var obj; 
    var test; 
    //code to determine test value 
    if (!test){ 
     return $q.when(obj); //<-- return $q.when 
    } 
    return getObj(url) 
     .then(function(obj){ 
      return obj 
     }) 
} 

var getObj = function() { 
    return $http.get(url);  
} 

var open = function() { 
    //this will always work now on 
    //return t(); should be enough as well 
    return t() 
     .then(function(obj) { 
      return obj; 
     }) 
} 

when(value):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.

+0

完全作品谢谢! – BonJon 2015-01-21 00:05:01

1

您可以检查t的类型是函数还是对象。为了被调用,它必须作为函数键入。

//this won't work for regular object, it has to be http object 
if(typeof t !== "function"){ 
//return; or handle case where t is a plain object 
} 
+0

我猜他是在谈论的承诺目标和非承诺对象。如果是非承诺对象't()。那么()'调用将失败。 – PSL 2015-01-21 00:01:08

+0

@PSL - 是的,我不确定open的意图是什么。由于该对象只能从那里传递,并且由于它最初设置为'{}',所以在这种情况下再次传递't'也许是有意义的,这就是为什么我试图指示'// return;'in这个答案。另一方面,我也喜欢你在承诺中包装对象的想法,在这种情况下,这可能是最佳实践。 – 2015-01-21 00:03:34

0

您可以验证返回的对象是否具有承诺的对象或不:

var open = function() { 
    var result = t(); 
    //Verify whether the return object has a promise object or not 
    if(angular.isObject(result.promise) 
    return result 
     .then(function(obj) { 
      return obj; 
     }) 
} 
+0

调用一个普通的对象会给你'未捕获的TypeError:对象不是一个函数'。 – 2015-01-20 23:53:53

1

修改后的代码

传递一个回调方法

var t = function(cb) { 
    var obj 
    var test; 
    //code to determine test value 

//return object depends on test value, 
//if test is undefined, return regular obj, 
//if not make a http request. 
    if (!test){ 
      cb(obj); 
    } 
    return getObj(url) 
     .then(function(obj){ 
      cb(obj) 
     }) 
} 

var getObj = function() { 
    return $http.get(url);  
} 

var open = function() { 
    //this won't work for regular object, it has to be http object 
    return t(function(obj) { 
      // write code dependent on obj 
     }) 
} 

var obj = open(); 
+0

cb做什么? – BonJon 2015-01-21 00:02:47

+1

传递回调的问题是您失去了承诺和承诺链接的优势。 – PSL 2015-01-21 00:03:17

相关问题