When calling the B function, call An if the A function is not executed, wait for the A function to be completed if it is being executed, and execute the B function code directly if it has already been executed.

for function A, the function is to obtain user data asynchronously. Success and fail are officially provided.
now there is a requirement that sometimes function An executes alone without the need to execute function B after successful execution, while function B needs to be executed after fetching user data.

I now set the mark gettingUserData for function A to mark that function An is executing, so for function B, if function An is executing, then there is no need to execute function An again, he just needs to wait for function A to execute successfully and then execute the code that B needs. I don"t know what to do here.

I hope to be able to execute B"s own code immediately after A"s execution is completed. What should I do when I use while (! window.getUserData) {} to find that it doesn"t work?

function A( resolve, reject ){
    var self = this
    window.gettingUserData = true
    sucess:{//
        window.getUserData = true
        resolve(  )
    },
    fail:{
        reject(  )
    }
}

function B(  ){
     var callBack = function(  ){
        //B
    }    

    var p = new Promise( A )
    if( window.getUserData ){//B
        callBack( )
    }else{
        if(  !window.gettingUserData ){//A
            p.then( callBack )
        }else{//A
            //AA
        }
    }
}
Jun.12,2021

take advantage of the principle that the Promise state will not change once determined, as long as it is the same Promise , you can complete your requirements.
name a chestnut

let promiseA;

function A(by = 'A') {
    if (!promiseA) promiseA = new Promise((reslove, reject) => {
        setTimeout(() => {
            console.log('A success by function', by);
            reslove(by);
            promiseA = null;
        }, 1000)
    })

    return promiseA;
}


function B() {
    A('B').then((data) => {
        console.log('run B recive', data)
    })
}

//

function test() {
    console.log('first test')
    A();
    A();
    setTimeout(() => {
        B();
    }, 500)


    setTimeout(() => {
        console.log('second test')
        B();
        setTimeout(B, 500)

    }, 2000)
}

test();
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-1b41f7c-2c5d0.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-1b41f7c-2c5d0.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?