最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Avoiding async: false; in jQuery AJAX within a function - Stack Overflow

programmeradmin0浏览0评论

Here's some pseudo code that I'm working with (and where'd I'd typically use async: false;):

function getData(){
    var obj = {};
    if(!settings.data){
        $.ajax({
            url: '/someendpoint',
            async: false,
            success: function(data){
                obj = data;
            }
        });
    } else {
        obj = settings.data;
    }
    return obj;
}

So we have a function that returns an object. Now if we already have some data, we just assign this data, but if we don't, we need to request the data from the server. A time to use async: false? Because we need to halt execution as to grab the data before we return the obj?

I deal with this concept time and time again, resorting to async: false most times. Can someone outline a better approach please?

Here's some pseudo code that I'm working with (and where'd I'd typically use async: false;):

function getData(){
    var obj = {};
    if(!settings.data){
        $.ajax({
            url: '/someendpoint',
            async: false,
            success: function(data){
                obj = data;
            }
        });
    } else {
        obj = settings.data;
    }
    return obj;
}

So we have a function that returns an object. Now if we already have some data, we just assign this data, but if we don't, we need to request the data from the server. A time to use async: false? Because we need to halt execution as to grab the data before we return the obj?

I deal with this concept time and time again, resorting to async: false most times. Can someone outline a better approach please?

Share Improve this question edited Sep 23, 2013 at 11:17 Fabrício Matté 70.2k27 gold badges133 silver badges167 bronze badges asked Sep 23, 2013 at 11:08 benhowdle89benhowdle89 37.5k74 gold badges207 silver badges340 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Embrace the asynchronous nature of the web. Granted, it's paradigm shift, but so was multi-threading back in the day and in my opinion the way it's handled now in jQuery with Deferred objects makes it preferable over multi-threading. It's the reason why Node.js is being so popular.

Don't just use callback functions as you might read on the web or in other answers. Learn about Deferred and Promise objects, and instead of returning the data, return a promise that you can use to attach behavior to when that promise is 'resolved'.

function getData(){
    var obj;
    if(!settings.data){
        obj = $.get('/someendpoint');
    } else {
        obj = $.when(settings.data);
    }
    return obj;
}

Then, the caller can use this data the moment it bees available, as such:

var promise = getData();
promise.done(function (data) {
    // Do something with the data.
});

It will feel awkward at first, because you're no longer returning data but instead a 'promise' to that data, but after a while you'll just 'get it'. Trust me.

Use a simple callback mechanism

function getData(callback){
    var obj = {};
    if(!settings.data){
        $.ajax({
            url: '/someendpoint',
            success: function(data){
                callback(data)
            }
        });
    } else {
        callback(settings.data)
    }
}

getData(function(data){
    //do things which depends on data
})
发布评论

评论列表(0)

  1. 暂无评论