如何使用长期解决方案替换异步错误的Ajax调用

时间:2018-05-01 19:07:51

标签: javascript jquery asynchronous jquery-deferred

我想用不使用async: false属性的代码替换以下代码。

//Iterate each item in the custom field
_.each(item_names, function(item_name) {

    //URL for Item API
    var searchURL = baseURL + $.trim(item_name) + '&fields=storedisplayname2,urlcomponent';

    $.ajax({
        url : searchURL,
        type : "get",
        async: false,
        success : function(data) {
            if(data.items.length == 1) //Using the Item API with a keyword parameter, we can't always guarentee that only 1 item will be returned
            {
                //Create a new "item" object using the returned JSON with 2 fields needed
                var newItem = {
                    'name': data.items[0].storedisplayname2
                ,   'url': SC.ENVIRONMENT.siteSettings.touchpoints.home + '/' + data.items[0].urlcomponent
                }
                styledWithItems.push(newItem);
            }
        },
        error: function() {
            return;
        }
    });

});

如果没有async: false属性,styledWithItems数组将为"空"稍后当试图访问它的值时,它将返回长度为0.

0 个答案:

没有答案