【发布时间】:2016-06-21 20:29:43
【问题描述】:
我有一个数组,其中可以包含未知数量的索引。每个索引用于通过ajax 调用发送数据。我正在循环使用for loop 从成功调用中收集数据并将其推送到一个空数组中。在未知数量的调用结束时,我需要在我的视图中使用新收集的数组。 newDataArray 在循环完成之前在底部执行,因此它仍然是空的。如何完成所有呼叫然后执行底部的操作?
如果有帮助,我将在 React 中使用 Flux 模式执行此操作。但同样的问题可能不会在 React 中完成。这是我正在尝试做的模拟示例:
JS
case 'execute-calls':
//This is the new array to push to
var newDataArray = [];
//Url to call
var url = 'http://dev.markitondemand.com/Api/v2/Quote/jsonp';
for(let i = 0; i < payload.data.length; i++){
//given array of data that needs to be sent with call
let symb = { symbol: payload.data[i]};
$.ajax({
data: symb,
url: url,
dataType: "jsonp",
})
.done(function(data){
let updatedData = {
//...data that is stored from response
};
newDataArray.push(updatedData);
})
.fail(function(error){
//console.log(error);
});
}
//This will be updating the state object which is above the switch cases
//However this is ran before the end of the loops so newDataArray is empty
var updateTicker = {
updatedTicker: true,
updatedTickerSymbols: newDataArray
};
assign(stockData,updateTicker);
getStockData.emitChange();
break;
【问题讨论】:
标签: javascript jquery reactjs flux