ChristopherW
ChristopherW

Reputation: 1083

When I return true in Javascript, it returns as undefined

I call a javascript function from another as show below, but every time isPassword gets called, it returns as 'undefined' and not True or False as intended. I print the values before they get returned and they are correct, but return true; doesn't work still. Any ideas?

function isPassword(pass){

var datatosend = {};
datatosend['api'] = 'checkpassword';
datatosend['version'] = 'v1';
datatosend['pass'] = pass;

jsonAPI(datatosend,function()
{
    if (typeof(responseVar['success'])=='boolean')
    {
        var ans = responseVar['success'];
        if (ans)
        {
            //Returns true to changePassword();
            return true;
        }
        else if (!ans)
        {
            //Returns false to changePassword();
            return false;
        }
    }
});
}

function changePassword(){

    //THE BELOW isPassword CALL IS UNDEFINED
    if(isPassword(oldPass)){
        successStatus.text("Current password not entered correctly");
        successStatus.css('color', 'red');
        clearTextAfter('changePasswordStatus', 5000);
        return false
    }
}

I can provide more code on request if it's more then a simple problem.

Upvotes: 0

Views: 1573

Answers (1)

jcaron
jcaron

Reputation: 17720

Not quite sure what jsonAPI is, but if it's an Ajax call, that's asynchronous, so the function itself ends after that function call (and there's no return value), while the anonymous function passed as an argument to jsonAPI will be executed a little bit later. You will need to do whatever you need with the response at that point, not when isPassword returns.

Upvotes: 2

Related Questions