The problem with promise

as soon as I came into contact with promise, I wrote a demo by myself. Ask

.

Why my then only executes once, and how to make it execute multiple times

    var test = 0;
    var promise = new Promise(resolve => {
        $(".btn").on("click",()=>{
            console.log(testPP +"----------")
            resolve(test)
        })
    })
    promise.then((data)=>{
        console.log(data+"*********")
    })

clipboard.png

Feb.28,2021

var test = 0;
$('.btn').on('click',()=>{
    console.log(testPP +"----------");
    Promise.resolve(test).then(successfn);
})

function successfn(data){
    console.log(data+"*********");
}

because Promise doesn't work this way, a Promise object can only be resolve or reject once, just like a request ( request ) you can only reply ( response ) once. If you must use Promise , you can only create a new Promise and then resolve every time click, but it doesn't make sense.

Menu