0

I have two nested functions like this:

var userhascompany;

$(document).ready(function(){

    checkforcompany();


        if (Object.keys(localStorage).length > 0) {

            getaddjobcache();

        }

    });

The checkforcompany(); function looks like this:

$.ajax({

    url: 'addjob_companycheck.php',
    success: function(dataresponse) {

        userhascompany = dataresponse;

    }

}); 

The function getaddjobcache(); should only get called, when the variable userhascompany has a value or rather the ajax request has finished.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • 2
    What about just returning a `Promise` from checkforcompany? – briosheje Apr 16 '19 at 07:03
  • Yeah, I watched some youtube content about promises and red some articles, but in every example the syntax of the code looks diffrent than what I am used to, which confuses me and I don´t understand it. So, I hope that someone could maybe do an example with my code environment, so I will maybe understand handling promises. – stefanhalbeisen Apr 16 '19 at 07:12

2 Answers2

2

Modify checkforcompany() to return the jqXHR created by $.ajax(), since it implements the thenable interface. I recommend using jQuery 3.x for consistent promise behavior, but if you must use an older version, you can usually get away with wrapping it in a call to Promise.resolve() in order to coerce the behavior to that of a compliant promise.

function checkforcompany () {
  return $.ajax({
    url: 'addjob_companycheck.php'
  });
}

$(() => {
  checkforcompany().then(dataresponse => {
    const userhascompany = dataresponse;

    if (Object.keys(localStorage).length > 0) {
      getaddjobcache(userhascompany);
    }
  });
});

Also don't rely on global variables in order to pass values between contexts. Make getaddjobcache() accept userhascompany as a parameter instead, as shown above.

Lastly, $() is recommended by jQuery instead of $(document).ready().

Patrick Roberts
  • 49,224
  • 10
  • 102
  • 153
1

There are two approaches for this one is using callback functions other is using .then block or async-await

Using Callback function

var userhascompany;
$(document).ready(function(){
    checkforcompany();
});

function checkforcompany(){
    $.ajax({
        url: 'addjob_companycheck.php',
        success: function(dataresponse) {
            userhascompany = dataresponse;
            if (Object.keys(localStorage).length > 0) {
                getaddjobcache();

            }
        }

    });
}

Using promise .then

var userhascompany;
$(document).ready(function(){
    checkforcompany()
    .then(function(dataresponse){
        userhascompany = dataresponse;
        if (Object.keys(localStorage).length > 0) {
            getaddjobcache();

        }
    })
});

function checkforcompany(){
    return new Promise(function(resolve, reject){
        $.ajax({
            url: 'addjob_companycheck.php',
            success: function(dataresponse) {
               resolve(dataresponse)
            }
            error: function(err) {
               reject(err)
            }
        });
    })
}

Using jQuery ajax .done

var userhascompany;
$(document).ready(function(){
    checkforcompany()
    .done(function(dataresponse){
        userhascompany = dataresponse;
        if (Object.keys(localStorage).length > 0) {
            getaddjobcache();

        }
    })
});

function checkforcompany(){
    return $.ajax({
            url: 'addjob_companycheck.php',
        });
    })
}
Ashish
  • 4,206
  • 16
  • 45