0

i am facing an issue. i have angularjs function which in turn call to another angularjs function which has post request.This post request always fire at last when first function ends..it is not fired sequntially.

        servicePOST.send(appConstants.BASE_MS_URL + 'Dcrs/activityDay.php',{
             "date":d
             }).then(function(result) {
        console.log(result);

         });

Please someone explain me this behaviour..any workaround for this...i want to execute all http request sequentially.how do i implemet it to this code? Thanks in advance!

Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29
Clyde Dias
  • 195
  • 1
  • 5
  • 18
  • You should look at using promises: http://www.dwmkerr.com/promises-in-angularjs-the-definitive-guide/ – Evan Bechtol May 02 '16 at 19:11
  • hmmm...i came to promises...now im confused to implement the code in my question with promises...any help will be appreciated – Clyde Dias May 02 '16 at 19:13
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Matthew Green May 02 '16 at 19:32

3 Answers3

0

As I understand from your question, you have a page where one section depends on other section and so on. and all these sections are being served/rendered by different-2 http request.

In this case, you can make the second http request from the success/resolved callback of first http request and so on. e.g.

servicePOST.send(appConstants.BASE_MS_URL + 'Dcrs/activityDay.php',{
         "date":d
         }).then(function(result) {
     $scope.dcrlocked = result.dcrlocked;
     $scope.leaves = result.leaves; 
     //$scope.holidays = result.holidays; 
     //make another http request as below.
     servicePOST2.send(url,{data or data from last request}).then(function(){
            // make another http request. and so on.
        })
     });

As all the http requests are being made from the success callback of last http request, it will guarantee sequential http requests.

EDIT

you can make use of $promise in your second function, where you are calling post request. e.g.

    var deferred = $q.defer();
    servicePOST.send(appConstants.BASE_MS_URL + 'Dcrs/activityDay.php',{
        "date":d
    }).then(function(result) {
        $scope.dcrlocked = result.dcrlocked;
        $scope.leaves = result.leaves; 
        //$scope.holidays = result.holidays; 
        deferred.resolve(result);
    });
    return deferred; // return deferred from your function.

Don't forget to inject $q in you controller and then passing it to second function. This will make post function to return synchronously. Let me know if this what you are looking for.

csjoshi04
  • 51
  • 7
  • i just want that code to be executed sequentially....nothing more...no more http requests in my code other than this – Clyde Dias May 02 '16 at 19:28
0

This can help you

var TestService = function( $scope, servicePOST, AnotherService2,  AnotherService3 )
    {
        // Level 1
        servicePOST
            .send(appConstants.BASE_MS_URL + 'Dcrs/activityDay.php',{
             "date":d })// Request #1
            .then( function( result )                // Response Handler #1
            {
                 $scope.dcrlocked = result.dcrlocked;
                // Level 2
                AnotherService2                             //AnotherService Call
                    .send({})        // Request #2
                    .then( function( result  )              // Response Handler #2
                    {
                       $scope.leaves = result.leaves; 
                        // Level 3
                        AnotherService3
                            .send({})      // Request #3
                            .then( function( result )          // Response Handler #3
                            {
                               //$scope.holidays = result.holidays;
                            });
                    });
            });
    };
Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29
  • edited the code in my question for simplicity...i want that piece of code to execute suquentially instead of it getting executed in the end(like how it behaves)...using promises....but i am confused how to implement MY code using promises....any help wud be appreciated – Clyde Dias May 02 '16 at 19:32
  • this code is execute in sequentialy.Promises are used to make async call in sequential way.In this i have include three services which are doing their respective functions. – Wasiq Muhammad May 02 '16 at 19:57
0

In short you can't. Javascript is non-blocking by design, you need to take a look at promises or implement nested callbacks.

Ricardo Gamba
  • 154
  • 1
  • 2