The correct solution does not make it synchronous (this is possible, but not recommended). It uses the callback appropriately. Asynchronous programming is addictive, but worth it.
Instead:
function foo() { ... contained = false; $.getJSON(URL, function ( data ) { $.each( data, function( i, item ) { if ( item.Info.code == code ) contained = true; }); });
make:
function getContained(containedCallback) { $.getJSON(URL, function(data) { var contained = false; $.each( data, function( i, item ) { if ( item.Info.code == code ) contained = true; }); containedCallback(contained); } ); } function foo() { ... getContained(function(contained) {
source share