Encapsulate an ajax request, which successfully returns a promise function, how to call the method in the page and execute the promise callback

clipboard.png

clipboard.png
this call will report an error that then is not defined

Mar.21,2022

is written as follows:

return new Promise(res, rej) {
    res(data);
}

you $.ajax itself returns a Promise you can directly return out. If you do not do data processing, there is no need to encapsulate a layer of Promise


.

Brother, you used it wrong. Look, I just wrote a rough idea. Promise wraps the whole thing

function get(){
    return new Promise(function(resolve,reject){
    let rootId = 
    
    $.ajax()
    
        if(data.status == 200){
            resolve()
        }
        else{
            reject()
        }
    })
}
get().then(res =>{}).catch(res=>{})
Menu