Maximus S
Maximus S

Reputation: 11095

Returning from nested functions

function outer() {
    $(['hi', 'there']).each(function(idx, e) {
        console.log(e);
        return;
    });
    alert("I don't want to be called");
}

function outer() {    
    $.get('http://test.com', function (data) {
        console.log(data)
        return; // I want to terminate the entire outer() function here.
    });
    alert("I don't want to be called");
}

What's the convention of breaking out of nested functions in cases like this? When using for loops, returning inside them terminates the entire function that encloses them. However, since $.each() is an individual function call, returning from it only ends itself, not the outer function. I could simply return twice, once inside and once outside in that case, but I am not sure how I would deal with $.ajax() because it is necessary to terminate the function only when I get a successful response.

Upvotes: 1

Views: 100

Answers (3)

Michael Geary
Michael Geary

Reputation: 28850

For $.each(), you can stop the iteration with return false; in the callback, as described in the jQuery documentation.

This won't return from the calling function, but you can set a flag and test it in the outer function.

If you want an easy way to return from the outer function from inside the loop, you're better off with a simple for loop:

var array = [ 'hi', 'there' ];
for( var i = 0;  i < array.length;  ++i ) {
    var e = array[i];
    console.log(e);
    return;
}
alert("I don't want to be called");

For $.get(), you should add some console.log() calls to your code and observe the order in which they are called:

function outer() {
    console.log( 'in outer before $.get is called' );
    $.get('http://test.com', function (data) {
        console.log( 'inside $.get callback' );
    });
    console.log( 'in outer after $.get returns' );
}

Now what you'll notice is the order of the log messages:

in outer before $.get is called
in outer after $.get returns
inside $.get callback

See how the callback is the last thing? It's called after the outer function finishes. Therefore, there is nothing this callback can do to prevent the rest of outer from executing.

So you need to think some more about what you need to do and figure out a different way to accomplish it.

Upvotes: 1

PeterKA
PeterKA

Reputation: 24638

Here is a summary of how it all works.

.each()

return true;  // skips to the next iteration of .each()
return false; // exits the .each() loop

In short there's no way of breaking out of the function containing .each() in a single statement.

$.get()

return [true|false]; // makes no sense at all.

As the return in $.get() does not get executed until the ajax call is complete, it wont serve much purpose. Even when you make a synchronous ajax call, a return statement in the success callback still does not do anything substantive. Think of a return from a function as a value to be assigned to the calling statement.

What's making it necessary to break out of your functions?

Upvotes: 1

Kent Anderson
Kent Anderson

Reputation: 486

If you use $.ajax() instead of the short hand $.get(), it is possible to specify that it be executed synchronously.

$.ajax({
  url: 'http://test.com',
  success: function (data) {
  },
  async: false
});

Typically, when you want to make a decision based on the return value of a function you have it return a value and then use a conditional statement to determine what happens next.

Upvotes: 0

Related Questions