BonJon
BonJon

Reputation: 799

How to return an object in my case?

I have a function that might return regular object or an http request object.

I have something like

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();

How to check if returned object is through http request or just a regular object?

Thanks for the help!

Upvotes: 2

Views: 103

Answers (4)

PSL
PSL

Reputation: 123739

If i understand correctly your issue is with the object returned by t is a promise or not to enable chaining. You could do always wrap the object with $q.when(obj) it will make sure object returned is always a promise and can be chained through. You need to make sure to inject $q the way you are doing $http. Or just wrap the test value itself with var obj = $q.when(value) and 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.

Upvotes: 3

Anoop
Anoop

Reputation: 23208

Modified code

Pass a callback method

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();

Upvotes: 1

lvarayut
lvarayut

Reputation: 15349

You can verify whether the return object has a promise object or not:

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;
        })
}

Upvotes: 0

Travis J
Travis J

Reputation: 82337

You could check if the type of t was a function or an object. In order for it to be called, it must be typed as a function.

//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
}

Upvotes: 1

Related Questions