javascript - How to return data obtained asynchronously
我想大声告诉你
我想大声告诉你 2017-05-16 13:12:20
0
3
455

As shown in the figure, the data is obtained asynchronously using ajax. How to return the data to the previous layer. . . As shown in the figure, how can we make the second return value asynchronously obtain the returned data

我想大声告诉你
我想大声告诉你

reply all(3)
仅有的幸福

The person above is right, use promise

get:function(){
    return new Promise(function(resolve,reject){
        //ajax...
        $.post("test.php",function(response){
            resolve(response)
        })
        //如果有错的话就reject
    })
}

Use

get().then(function(response){
    //response
}).catch(function(err){
    //错误处理
})
Peter_Zhu

Either change it to synchronous or use callback, your return is useless

get:function(callback){
    $.post(.....,function(res){
        callback(res)
    })
}

get(function(res){
    console.log(res);
})
Ty80

Make it a Promise

return Promise.resolve($.post(url,data));
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template