2

I need to make sure that getValue() is called only after execution of SetValueForVariable(). I cannot modify BindValue() or SetValueForVariable().

I tried $.when(BindValue()).then(getValue());. (As i cannot change the already existing flow) It works some times but sometimes it shows the previously set value.

I need to call getValue on $(document).ready(). How do I make sure getValue() is only called after execution of SetValueForVariable().

//This function is inturn making call to SetValueForVariable() which is written in another file
//Cannot Change this
  function BindValue() {

    SetValueForVariable()

  }

  function getValue()
  {
    $.ajax({
    url: getRoutePath("GetPath/GetPath1"),
    type: "GET",
    async: true,
    success: function (data) {
    debugger;
        data = JSON.parse(data)
        $('#txtBox').text(data.Count);

        });
   }


//Written in another file  
//Cannot change this function
function SetValueForVariable() {


//this fucntion is making server call the server method sets value of a Session Variable
   $.ajax({
   url: getRoutePath("SetPath/SetPath1"),
   type: "GET",
   async: true,
           ....

     });
  }
3
  • 1
    This is not standard JavaScript. Please flag similar posts also as "jQuery" (fixed this one for you). Commented Aug 13, 2015 at 7:34
  • 1
    $.when need a parameter, a Deffered, and your functions return nothing. You have to return $.ajax Commented Aug 13, 2015 at 7:35
  • Unfortunately SetValueForVariable doesn't return the result of $.ajax (and BindValue doesn't return anything either) - so there's no possibility to use promises. What does setvalueforvariable do in it's success method? maybe you could kludge something together if it does something interesting there - but, I reckon you're SOOL Commented Aug 13, 2015 at 7:36

1 Answer 1

3

You could redefine BindValue, calling the original BindValue in your new definition after ensuring that getValue has been called.

Pseudo code

 var originalBindValue = BindValue;

 BindValue = function() {
      if getValue has been called 
           originalBindValue();
      else 
           call getValue and then originalBindValue() in getValue success / failure callback
 }

I think this would work around your cannot modify BindValue limitation - you don't actually need access to BindValue code here.

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

1 Comment

This is the best you can do in this case if BindValue cannot be modified directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.