Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have some functions to fetch data and to post data to server but due to asynchronous nature of angular js the next method start executing before the previous one returns which returns empty string it is used in next functions...i have read a lot articles about promise and $q and non of them worked for me............

i want the fuctions execute in the way they are written is there is any way? pls help......

my code runs on my local system perfectly but it is not running the expected way on Server(cloud)

pls help...

What I have tried:

JavaScript
app.factory('myService', function ($http) {
    return {
        // 1st function
        serverCall: function (purchaseid) {
            return $http.get('/po/poidsearchid/' + purchaseid).then(function (response) {
                console.log(response.data);
                console.log("Hi in server call");
                return response.data;
            });
        },
        // 2nd function
        anotherFunctionCall: function (poid) {
            //console.log("Hi in another call");
            $http.post('/po/poid', poid);
               console.log("Hi in another call");
            

        },

        // 3rd function
        anotherFunctionCall1: function (poiditems) {
           
            $http.post('/po/poiditems', poiditems)
                console.log("Hi in another call 1111");
                 
            

        },

        anotherFunctionCall2: function (purchaseid) {
            
            return $http.get('/po/poiditemssearchid/' + purchaseid).then(function (response) {
                console.log(response.data);
                console.log("Hi in another call 222");
                return response.data;
               
            });

        }
Posted
Updated 3-Mar-17 20:44pm
v5

1 solution

Here is a google search that should give you answers on AngularJS asynchronous programming: async angularjs - Google Search[^]
 
Share this answer
 
Comments
Member 12763843 4-Mar-17 2:47am    
thnx for your reply... i have already gone through several links...gonna check this one...
Graeme_Grant 4-Mar-17 3:12am    
This is what you are looing for from the link provide above (first result returned): AngularJS Corner - Async calls using $http, $q and promises[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900