-1

I have created following function in AngularJS

      var enq_dt = new Date();

        $.post("/api/EMSAPI/EnquiryDetails?enq_no="+o_enq_no, null, function (returnedData) {
            enq_dt = returnedData["D_O_O"];
            console.log("Loading Post Block");
            console.log(enq_dt);
        });


        console.log("Loading General Block ");
        console.log(enq_dt);
        $scope.CurrentQuotation = {
            EnquiryNo:o_enq_no,
            EnquiryDate: enq_dt,
            QuotationBy:"TEST"
        };

I am getting following result in console window. Loading General Block 2010-11-26T00:00:00 Loading Post Block 2010-12-12T00:00:00


I want to Load Post block first and after that I want to run General Block.

What I am missing (I am new to Angular) ?

Thanks in advance.

  • 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) – Tamas Hegedus Nov 26 '16 at 13:59

1 Answers1

0

I suggest you Google the word "asynchronous". In JavaScript, things like HTTP requests are almost always asynchronous.

To get your general code to run after the post, call it with .then():

function generalCode() {
    console.log("Loading General Block ");
    console.log(enq_dt);
    $scope.CurrentQuotation = {
        EnquiryNo:o_enq_no,
        EnquiryDate: enq_dt,
        QuotationBy:"TEST"
    };
}

var enq_dt = new Date();

$.post("/api/EMSAPI/EnquiryDetails?enq_no="+o_enq_no, null)
   .then(function (returnedData) {
       enq_dt = returnedData["D_O_O"];
       console.log("Loading Post Block");
       console.log(enq_dt);
   })
   .then(generalCode);
JLRishe
  • 99,490
  • 19
  • 131
  • 169