2015-01-20 2 views
2

정규 개체 또는 http 요청 개체를 반환하는 함수가 있습니다.내 경우에 개체를 반환하는 방법?

나는 반환 된 개체는 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(); 

같은 것을 어떻게 확인할 수 있나요?

도움 주셔서 감사합니다.

답변

3

전달 문제는 t에 의해 반환 된 객체와 함께 약속을하거나 활성화하지 않습니다. haining. 항상 $q.when(obj)으로 객체를 래핑 할 수 있습니다. 반환 된 객체가 항상 약속이며 체인을 통해 연결될 수 있는지 확인합니다. 당신은 $q$http하고있는 방식으로 주입해야합니다. 아니면 테스트 값 자체를 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

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(). then()'호출은 실패 할 것이다. – PSL

+0

@PSL - 그렇습니다. 오픈의 의도가 무엇인지 확신 할 수 없었습니다. 객체가 거기서부터 전달되고 있기 때문에 원래'{}'로 설정되어 있었기 때문에이 경우't'를 다시 전달하는 것이 합리적이었습니다. 그래서'// return; '을 표시하려고했습니다. 이 대답. 다른 한편으로는, 나는이 경우 아마 모범 사례 인 약속에 객체를 배치하는 아이디어를 좋아합니다. –

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 : 객체가 함수가 아닙니다.'라는 메시지가 나타납니다. –

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

+1

콜백을 지나가는 문제는 약속의 이점을 잃어 버리고 체인 연결을 약속한다는 것입니다. – PSL

관련 문제