8

I have a caller function that invokes another function that send a HTTP POST with parameters. Now i want that this called function blocks execution until there is its "success" (so when its HTTP POST has been done).

This is my logical code:

var fingerprint = null;
var janus_session = null;
var inserted = "false";

$(document).ready(function() {
      //stuff
      fingerprint = FindFingerprint(jsep);

      janus_session = janus.getSessionId();
      inserted = SendSDPLine(fingerprint, janus_session);
      console.log("**in MAIN: inserted= " + inserted);

      //other stuff
    }

function SendSDPLine(fingerprint, janus_session) {
  var sdp = fingerprint;
  //    var url = "http://localhost:8484/Shine/AccountController";
  var action_type = "InsertSDPLine";
  var sessionid = janus_session;

  $.ajax({
    type: "POST",
    url: url,
    xhrFields: {
      withCredentials: false
    },
    data: {
      "action": action_type,
      "sdpline": fingerprint,
      "sessionid": sessionid
    },
    success: function(data) {
      if (data == "INSERTED") {
        inserted = "true";
        console.log("in SENDSDPLINE: inserted= " + inserted);
      }
      return inserted;
      //        return checkFingerprint (fingerprint);
    },
    // vvv---- This is the new bit
    error: function(jqXHR, textStatus, errorThrown) {
      console.log("Error, status = " + textStatus + ", " +
                  "error thrown: " + errorThrown);
    }
  });

}

In few words, i want that other stuff has been executed after that HTTP POST response has been checked. I've already seen another problem: initially, inserted has false value. In success(data) in HTTP POST response, it has true value. But, in the caller function, in the following console.log has undefined value.

So, i have two question:

  1. how to return this value to the caller function
  2. how to stop execution of the caller function until HTTP POST response has been received?
1
  • 1
    Maybe async/await can help you here. But if not, then it's impossible (to stop execution of the caller function) and you have to resort to using promises or callbacks. Commented Apr 20, 2017 at 8:38

2 Answers 2

2

If you need to block the execution until AJAX returns, you can specify async:false in ajax parameters, as per jQuery documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

Use callback function

var fingerprint = null;
var janus_session = null;
var inserted = "false";


$(document).ready(function() {

//stuff

fingerprint = FindFingerprint(jsep); 


janus_session = janus.getSessionId();

//Use callback funcion to access 
SendSDPLine(fingerprint,janus_session , function(error,inserted){

  if(error){
     console.log("**Error in : "+error);
  }

  console.log("**in MAIN: inserted= "+inserted);


});


//other stuff

}

function SendSDPLine(fingerprint,janus_session, callback){

  var sdp=fingerprint;
//    var url = "http://localhost:8484/Shine/AccountController";
var action_type = "InsertSDPLine";
var sessionid = janus_session;

$.ajax({
  type:    "POST",
  url:     url,
  async: false, 
  xhrFields: {
    withCredentials: false
  },
  data:{
    "action"      : action_type,
    "sdpline"     : fingerprint,
    "sessionid"   : sessionid
  },
  success: function(data) {
    if (data == "INSERTED") {
      inserted = "true";
      console.log("in SENDSDPLINE: inserted= "+inserted);

      //return result
      return callback(null, inserted);
    }



      //        return checkFingerprint (fingerprint);

    },
    // vvv---- This is the new bit
    error:   function(jqXHR, textStatus, errorThrown) {
      console.log("Error, status = " + textStatus + ", " +
        "error thrown: " + errorThrown
        );

      //return error
      return callback(errorThrown, null);
    }
  });

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.