The value returned by the js function call?

questions are as follows

<input type="text" ng-model="roleName" bg-blur="checkRole(roleName)">
var isCheck = false;
function checkName(roleName) {
    $http({
        url: "http://...",
        type: "post"        
    }).then(function(res){
        isCheck = res.data[0] == 0 ? true : false;
    }, function(err){
        //
    }).then(
        return isCheck;
    );    
}

$scope.checkRole = function (roleName) {
    if(checkName(roleName)) {
       /// 
    }
}

Why is it judged to be undefined in the if (checkName (roleName)) condition, and find the reason?

Mar.02,2021

js is executed asynchronously.
needs to be converted to Synchronize.


$scope.checkRole = function (roleName) {

var a = checkName(roleName);
if(a) {
   /// 
}

}

There is an out-of-step in the

checkName method, so when you call it, it does not return the data you want from the $http. There is no other return in the method, so when there is no return in the method, the method defaults to returning undefined..
if you want this feature, you are recommended to use async await

Menu