javascript - 异步获取的数据怎么return出去
我想大声告诉你
我想大声告诉你 2017-05-16 13:12:20
0
3
419

如图,用ajax异步获取到了数据,怎么把数据返回到上一层。。。如图所示,怎么才能让第二个return的值是异步获取返回的数据

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!