0
var flow;

$.ajax({
    url: "qa/version.json",
    dataType: "json",
    success: function( response ){ 
        flow = response.Version;
    }
});

$(".flow").append(flow);

Due to the nature of JS asynchronous design, the append would will be execute before it is being assigned a value in ajax call. What is the best way to tell the script to wait until flow gets assigned in ajax call, then do the append? I do not want to put append right below the success, I would like to keep them separate.

5
  • 1
    Place $(".flow").append(flow); inside of success funciton Commented Mar 17, 2014 at 19:36
  • 2
    Please explain why you want to keep the success callback and the .append() call separate. Doing so is the correct approach. Commented Mar 17, 2014 at 19:38
  • Also explain why there is a requirement for this to wait until flow is assigned. Commented Mar 17, 2014 at 19:40
  • Because I am not just assigning flow, say if I have 50 other parameters from json to get appended. I just think it's kinda messy, and I like to have all the appends in one place. Commented Mar 17, 2014 at 19:43
  • @TerryChen You can always reference a function by name there... you don't have to physically put the code there. Alternatively, see my answer below for simply adding this as a handler for when the Deferred object is resolved. Commented Mar 17, 2014 at 19:45

4 Answers 4

4

The "best way" is to perform the action in response to the asynchronous action:

$.ajax({
    url: "qa/version.json",
    dataType: "json",
    success: function(response){ 
        $(".flow").append(response.Version);
    }
});

If you want to "keep them separate" then you can define a function to call in the response:

var appendFlow = function (flow) {
    $(".flow").append(flow);
};

$.ajax({
    url: "qa/version.json",
    dataType: "json",
    success: function(response){ 
        appendFlow(response.Version);
    }
});

Separating the code into its own function is simply a matter of organizing your code into re-usable components. Either way, by design the response can't be processed until it's received, so you'd perform your actions in response to the asynchronous call.

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

Comments

2

Anything wrong with:

$.ajax({
  url: "qa/version.json",
  dataType: "json",
  success: function( response ){ 
    flow = response.Version;
    $(".flow").append(flow);
  }
});

2 Comments

He specifically says he doesn't want to do that... but who knows why.
Because I am not just assigning flow, say if I have 50 other parameters from json to get appended. I just think it's kinda messy, and I like to have all the appends in one place
0

I have no idea why you don't want to put your success handler in the spot for a success handler, but here's an alternative that may help you.

jQuery returns a Deferred instance when you make AJAX requests. You can use its .done() method to set up a callback later.

var dfd = $.ajax( /* your code here, without the success handler */);

// later on...
dfd.done(function (response) {
  $('.flow').append(response.Version);
});

See also:

Comments

0

Or:

var request = $.ajax({
    url: "qa/version.json",
    dataType: "json"
});

request.done(function(response){
    $(".flow").append(response.Version);
});

Comments

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.