我想用不使用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.