Vue hangs a function in prototype, calling return is always undefined

this is the function

//
function CompareDate(d1)
{
    return ((new Date(d1))) > (new Date());
}
//
Vue.prototype.checkValidity = function(type){
    let result
    API.validity(function (res) {
    if(res.code == 1){
        if(type == "z"){
            result = (CompareDate(res.data.zin_expiring))
            console.log( "z",result)
            return result
        }
        if(type == "s"){
            result = (CompareDate(res.data.sync_expiring))
            console.log( "s",result)
            return result
        }
    } else {
        console.log(res.msg);
    }
    },{
        user_id: localStorage.getItem("user_id")
    })

}
//
console.log( "",this.checkValidity("z"))

output result

Feb.08,2022

as mentioned above, checkValidity itself does not have a return statement, so its return value is of course undefined. Your return is simply the return of an anonymous function that passes in the API.validity method in checkValidity.

because API.validity is an asynchronous function, adding return result to the API.validity logic directly returns before result is assigned. To get checkValidity to return as the result of the inner function, the simpler modification involves the return value of API.validity itself, if it now reads:

  

API.validity is preceded by return

Menu