I am having an issue within one of my reducers where I have all of the information readily available to return to the front-end except for one array called "items" which I need to do an additional fetch to get more data back. The issue I am having with this fetch is that it ultimate leads to a return of a promise instead of returning the value. My question is how can I get the value instead of the promise? (e.g. via blocking everything else until the promise resolves and then sending that back).
I have commented throughout the below code to hopefully explain everything more clearly:
export const getItemsForHistory = createSelector(
[getAllHistory, getAllItems, getHistoryPage], (history, items, page) => {
let itemIds = `[a bunch of ids]`;
//this is the call I am making to fetch the additional data for "items"
//this return statement is sending back a promise and not a value
return getUpdatedItems(itemIds, items).then(result => {
//result is the actual value I want
return result;
}, error => {
//if the fetch fails, I would like to send a default value for "items"
//note, if I place this return outside of this entire promise and remove the promise, it works as expected with the default data
return history.map(h => items.get(h.get('item'), null))
}).catch(error => {
return history.map(h => items.get(h.get('item'), null))
})
}
)
//I would like for this method to run asychronously
const getUpdatedItems = async (itemIds, items) => {
let result = await fetchHistoryItemsDetail(itemIds);
return result;
}
const fetchHistoryItemsDetail = (itemIds) => {
let headers = {
"Content-Type": "application/json",
},
fetchUrl = `XXXXXXXXX`;
return fetch(fetchUrl, {
method: "POST",
headers: headers,
body: itemIds,
withCredentials: true,
crossDomain: true,
})
.then(response => {
return response.json();
})
.catch(error => {
throw error;
})
}
export const getSortedHistory = createSelector(
[getAllHistory, getItemsForHistory, getHistorySort, getHistoryPage], (history, items, sort, page) => {
//this is where items is ultimately retured to the front end but it is now a promise and not an array of data
//(from getItemsForHistory)
return sortedEntities(history, items, sort)
}
)
I hope that my question is clear, but please let me know if further elaboration is needed.