简体   繁体   中英

q.all function not working as I want it too

I have the following code:

        function start() {
            var function = this.buildUpModels;
            console.log('async wordt gestart');
            $q.all([Timeline.updateEvents(),Timeline.updateTimeslots(),Sponsors.updateSponsors(),Biography.updateBiography()]).then( function (rv){               
                console.log('async is done');
                function ();
            });               

            window.localStorage.setItem('check', 'done');
            window.localStorage.setItem('planning', '[1,2]');
        } 

        function buildUpModels() {
            console.log('start buildUpModels')             
            Biography.buildObject();
            Timeline.buildObject();
        } 

The following is one of the async update functions (all are coded similarly):

 function updateBiography() {
            return $http.get("pathToData")
                        .then(function (resp) {
                            console.log( "update bio")
                            window.localStorage.setItem('biography', resp.data);
                            window.localStorage.setItem('biographyTimeStamp', Date.now());
                        }, function (err) {
                            console.log('ERR', err);
                        });
        }

As I understood it, both the console.log("async is done") and function() statements should be called after all promises within q.all() have been resolved. The functions are called before the async functions are resolved however. What exactly am I doing wrong?

I think I have found the answer! This problem arised purely because I was testing with ionic lab.

-Device nr.1 finds an empty localstorage and fills it. Then it starts building the objects. (And apparently it waits for the results as it should)
-Device nr.2 however finds a full localstorage (because of the other phone filling it) and then exeutes this.buildUpModels directly (as it is not called via the callback of an asynchronous function if the localstorage is not empty).

Tip: Watch out with testing code containing localstorage when using a testing tool like ionic lab

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