Sjoerd de Wit
Sjoerd de Wit

Reputation: 2413

Get promise data from angular $http

Hey i'm trying to build some default validation for $http promises. The reason i do this is because i have multiple requests that return the same result structure. so it would be logical to seperate the handler from the request to reduce code duplicates

Let's say i have this request:

    getMoreUsers: function(searchid, apiroute, limit) {
        console.log(apiroute);
        return $http({
            method: 'get', 
            url: WeAlertApiUrl + apiroute,
            params: {
                'searchid' : searchid,
                'start' : limit }
        }).then( function(data) {
           if(data.data.isValid === true) {
              return processUsers(data.data.users);
           }else {
              $cordovaToast.showShortTop(data.data.message).then(function(success) {
              }, function (error) {alert(data.data.message);});
           }
        }, function(error) {
            $cordovaToast.showShortTop("something went wrong..").then(function(success) {
            }, function (error) {alert("Something went wrong..");});
        }); 
    },

The only thing that isn't consistent is data.data.users so ideally i'd like to construct something like this:

getMoreUsers: function(searchid, apiroute, limit) {
        console.log(apiroute);
        return $http({
            method: 'get', 
            url: WeAlertApiUrl + apiroute,
            params: {
                'searchid' : searchid,
                'start' : limit }
        }).then( 
           return handleResult(processUsers(data.data.users))
        }); 
    },

Is this achievable? and how do i access the promise of $http and send it to a service

Thanks to @Bergi i was able to solve it! the getmoreusers service now looks like

    getMoreUsers: function(searchid, apiroute, limit) {
        console.log(apiroute);
        return $http({
            method: 'get', 
            url: WeAlertApiUrl + apiroute,
            params: {
                'searchid' : searchid,
                'start' : limit }
        }).then(function(res){return successHandler(res, 'users');}, errorHandler);
    },

and the success and error handlers are wrapped like :

.factory('successHandler', function($cordovaToast, processAlerts, processUsers, processBerichten, processReacties, processUserAlerts) {
        return function(data, type) {
            if(data.data.isValid === true){
                switch(type){
                    case 'users':
                        return processUsers(data.data[type]);
                    case 'alerts':  
                        return processAlerts(data.data[type]);
                    case 'berichten':   
                        return processBerichten(data.data[type]);
                    case 'reacties':    
                        return processReacties(data.data[type]);
                    case 'useralerts':  
                        return processUserAlerts(data.data[type]);              
                }
            }else {
                $cordovaToast.showShortTop(data.data.message).then(function(success) {
                }, function (error) {alert(data.data.message);});
            }   
        };
    }).factory('errorHandler', function($cordovaToast) {
        return function(error) {
            $cordovaToast.showShortTop("Er ging iets mis... bent u verbonden met het internet?").then(function(success) {
            }, function (error) {
                alert("Er ging iets mis... bent u verbonden met het internet?");
            });
        };
    })

Upvotes: 0

Views: 383

Answers (1)

Bergi
Bergi

Reputation: 665344

No, returning data.data.users is totally fine. You'll want to return the promise for the processed data, so that your callers can consume them like

getMoreUsers(…).then(handleResult);

For that, the global error handlers should also rethrow the exceptions

getMoreUsers: function(searchid, apiroute, limit) {
    console.log(apiroute);
    return $http({
        method: 'get', 
        url: WeAlertApiUrl + apiroute,
        params: {
            'searchid' : searchid,
            'start' : limit }
    }).then(function(data) {
        if (data.data.isValid) {
            return processUsers(data.data.users);
        } else {
            throw new Error(data.data.message);
        }
    }, function(error) {
        throw new Error("something went wrong.."); // ignore `error` details
    }).catch(function(error) {
        $cordovaToast.showShortTop(error.message).catch(function(showError) {
            alert(error.message);
        });
        throw error;
    }); 
},

Upvotes: 1

Related Questions