0

I have a function where in I execute 2 functions. When I view it in the browser, I get 2 loading symbols which i use for showing the loading of data.

I wanted to know if there is a way to execute each function, one after the other, since I can avoid the 2 loading symbols. I don't want to call the second function inside the first function as a solution.

The code is as shown below:

function ABCD() {
   function x();
   function y();
}

I want function x() to complete its execution and then start with function y()

5
  • that is the default nature of javascript.... with one exception if x has an asynchronous method call then execution of y will not wait for the asynchronous method to finish Commented Oct 31, 2014 at 12:17
  • Use jQuery promises and then to chain them sequentially or $.when to wait for them all to finish. Please show the rest of your code to get a better example. Commented Oct 31, 2014 at 12:18
  • Make sure to hide the content at the end of the first funciton. Then show the other content in the second function and hide it again at the end of the second function. Are you handling it in that way? Commented Oct 31, 2014 at 12:22
  • @plalx: going by the use of the term "loading symbols", I think we can assume ajax calls inside the two functions. Commented Oct 31, 2014 at 12:27
  • yes i make synchronous calls inside each of those functions Commented Oct 31, 2014 at 12:35

2 Answers 2

3

Assuming your functions return jQuery promises, you can do something like this:

function ABCD() {
   // return the sequential promises
   return x().then(y);  // or return x().done(y); if you want a result after first one only
}

then outside you can wait for both with a call like this:

ABCD().done(function(){
    alert("both complete");
});

if x and y make an ajax call just return that, as it is already a promise:

e.g.

  function x(){
      return $.ajax(...);
  }

  function y(){
      return $.ajax(...);
  }

Working Example JSFiddle: http://jsfiddle.net/TrueBlueAussie/9e5rx2bx/2/

Note: the example uses Deferred and setTimeout to simulate the ajax loads. Have the console open to see the log activity.

The older (non-promise way) would be using callbacks, however the exact implementation depends on your actual current code (which is not shown):

This is a preferred way of solving this type of issue since jQuery 1.5.

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

4 Comments

i get an error with using "then" or "done". can you please explain me what promise is.
@Nikki: What version of jQuery are you using? Promises are a relatively recent addition.
@Nikki... Does the JSFiddle work for you? (it should). We would need to see the rest of your code to see how you are using the above suggestions/samples.
Downvoter: please try the fiddle yourself before assuming anything :P This code works fine.
1

Here is a vanilla js option (note the callback function is x() has been made optional (i.e. x() executes just fine without a callback). DEMO

function y() {
    //execute code
}

function x(callback) {
    //execute code
    if (typeof(callback) === 'function') {
        callback();
    }
}

function ABCD() {
   x(y());   
}

ABCD();



Alternate version with @TrueBlueAussie's suggestion: DEMO2

function x(callback) {
    //execute code
    if(callback) callback()
}

4 Comments

if(callback == undefined) will blow up immediately on the next line.
@TrueBlueAussie good catch... I was testing it in the fiddle and swapped the != for ==
I realized that typeof(callback) === 'function' will be way more reliable and better suited to this use case
it is usually sufficient to test only if (callback) callback() or even the simpler callback && callback()

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.