简体   繁体   中英

Random results from assynchronous queries on REST-api

I created following controller with 3 differents $http GET calls to a rest-api.

$http({method: 'GET', url: REST_CONFIG.url+'/api/lims/data/runs/list'})
        .success(function(data, status, headers, config) {
            form.runs = data;
        })
        .error(function(data, status, headers, config) {
            form.runs = [];
        });
        form.data.analysis = {"analysisName": "","analysisprofile": {"workflows": []},"run": ""};
        //Get all Default Workflows
        $http({method: 'GET', url: REST_CONFIG.url+'/api/workflows/default/list'})
        .success(function(data, status, headers, config) {
            form.workflows = data;
        })
        .error(function(data, status, headers, config) {
            form.workflows = [];
        });
        //Get all databases
        $http({method: 'GET', url: REST_CONFIG.url+'/api/list-databases'})
        .success(function(data, status, headers, config) {
            form.databases = data;
        })
        .error(function(data, status, headers, config) {
            form.databases = [];
        });

Sometimes I have the same results from query1 and query2 (query2 have the result from query1). In that case the rest-api do 2 times the query1. My browser say that the http queries are good (3 differents url). This is weird and really annoying. I also tried to do:

        //Get all runs
         runs =  $http({method: 'GET', url: REST_CONFIG.url+'/api/lims/data/runs/list'});
         //Get all Default workflows
         defaultWorkflows = $http({method: 'GET', url: REST_CONFIG.url+'/api/workflows/default/list'});
         //Get all databases
         databases = $http({method: 'GET', url: REST_CONFIG.url+'/api/list-databases'});
         $q.all([runs, defaultWorkflows, databases]).then(function(values) {
            form.runs = values[0].data;
            form.workflows = values[1].data;
            form.databases  = values[2].data;
         });

Nothing worked. Is it coming from the rest-api? Or I am doing something wrong?

EDIT Problem solved. The key point was the use of $q with promise and deffer(). This plunkr helped me a lot: http://plnkr.co/edit/NGMp4ycmaCqVOmgohN53?p=preview

I use the following code:

    var getInfo = function(){
        var promises = [];
        var urls = [];
        urls.push(REST_CONFIG.url+'/api/lims/data/runs/list');
        urls.push(REST_CONFIG.url+'/api/workflows/default/list');
        urls.push(REST_CONFIG.url+'/api/list-databases');
        angular.forEach(urls, function(url){
            var deffered = $q.defer();
            $http({method: 'GET', url: url})
            .then(function successCallback(response) {
                deffered.resolve(response.data);
            }, function errorCallback(response) {
                deffered.reject();
            });
            promises.push(deffered.promise);
        })
        return $q.all(promises);
    }


     var init = function(){
         var promiseInfo = getInfo();
         promiseInfo.then(function(datas){
             form.runs = datas[0];
             form.workflows = datas[1];
             form.databases = datas[2];
         })
    };

You need to be sure to wait for all of the requests to finish before using forms , for instance, by using $q.all . Also, be sure to use then and catch ; success and error were deprecated back in v1.5:

function getInfo() {
    // Assuming `form` exists here...
    var promises = [];
    promises.push(
        $http({method: 'GET', url: REST_CONFIG.url+'/api/lims/data/runs/list'})
            .then(function(data, status, headers, config) {
                form.runs = data;
            })
            .catch(function(data, status, headers, config) {
                form.runs = [];
            })
    );
    form.data.analysis = {"analysisName": "","analysisprofile": {"workflows": []},"run": ""};
    //Get all Default Workflows
    promises.push(
        $http({method: 'GET', url: REST_CONFIG.url+'/api/workflows/default/list'})
            .then(function(data, status, headers, config) {
                form.workflows = data;
            })
            .catch(function(data, status, headers, config) {
                form.workflows = [];
            })
    );
    //Get all databases
    promises.push(
        $http({method: 'GET', url: REST_CONFIG.url+'/api/list-databases'})
            .then(function(data, status, headers, config) {
                form.databases = data;
            })
            .catch(function(data, status, headers, config) {
                form.databases = [];
            })
    );
    return $q.all(promises).then(() => form);
}

That returns a promise that will resolve to form when all three of the requests are complete (it won't reject, because rejections are turned into resolutions via the catch handlers, in keeping with your original code).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM