我正在调用后端服务以从该loadContent
方法的第一次获取中获取所有可用的 IDS,并且我正在从后端获取所有 IDS。
从初始服务中随机选择 10 个 IDS,我单独调用另一个服务来获取 ID 的完整数据。我也能够获取所有数据,但在获取所有数据之前,会调用带有调度的函数,因为数据未在存储中设置。
export function loadContent(requestUrl) {
return dispatch => {
return fetch( /* initial calls to get all the ids */ ).then((response) => {
return response.json();
}).then((data) => {
console.log(data);
var stories = [];
var x = 0;
var loopArray = function(data) {
return customAlert(data, function() {
});
}
function customAlert(topStories, callback) {
var randomNumber = topStories[Math.floor(Math.random() * topStories.length)];
fetch( /* call service with individual id */ ).then((response) => {
return response.json();
}).then((output) => {
console.log(output);
stories[x] = output;
x++;
// any more items in array? continue loop
if (x <= 10) {
loopArray(data);
} else {
return stories;
}
})
}
return loopArray(data);
}).then((stories) => {
dispatch({
type: "LOAD",
payload: stories
});
}).catch((err) => {
console.log("There has been error");
})
};
}
export function load(news) {
return {
type: 'LOAD',
news: news
}
}