The problem of return value in async in js.

the URL that gets the video information is returned asynchronously through the API background. So you need to get the url through the getDashInfo method, and then create the axios object, but the REST_BASEURL variable below is a Promise object, so let"s see where the code needs to be modified.

clipboard.png

function getDashInfo(){
    return new Promise((resolve,reject)=>{
        getDashServerInfo().then(res=>{
            console.log(res);
            let dash;
            let errCode=res.errCode;
            if(errCode==0){
                dash=res.data;
            }
            resolve(dash);
        })
    })
}
async function getBaseURL(){
    let dash=await getDashInfo();
    let url="";
    let dataPort="";
    if(dash.url.indexOf("https")!=-1){
        dataPort=dash.httpsCmdPort;
    }
    else{
        dataPort=dash.httpCmdPort;
    }
    url=dash.url+":"+dataPort;
    return url;
}
let REST_BASEURL=getBaseURL();
const newAxios=axios.create({
    baseURL:REST_BASEURL,
});
Jun.16,2022

async returns the promise object


The

getDashInfo function is rewritten in this way

function getDashInfo(){
    return getDashServerInfo().then(res=>{
            console.log(res);
            let dash;
            let errCode=res.errCode;
            if(errCode==0){
                dash=res.data;
            }
            return dash;
        });
}

//:
let REST_BASEURL=getBaseURL(); //async  promise
const newAxios=axios.create({
    baseURL:REST_BASEURL,
});
//:
getBaseURL.then(REST_BASEURL=>{
    const newAxios=axios.create({
        baseURL:REST_BASEURL,
    });
})
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e759dc-46336.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e759dc-46336.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?