There’s an example in MDN here,
const colors = fetch("../data/colors.json").then((response) => response.json());
export default await colors;
However, I have multiple constants that are populated asynchronously in a single function:
const _receiversById = {};
const _receiversByServer = {};
const _sequenceReceivers = {}; // name - k-v receivers
const _sequences = {};
// init
async function _fetchAllReceivers(){
// go to the database and copy data into those consts. multiple awaits.
}
I’m puzzled how do I make the constants the promises that get resolved while the function runs.
I was trying to assign the constants to the promise objects without parameters
const _receiversById = new Promise();
const _receiversByServer = new Promise();
const _sequenceReceivers = new Promise();
const _sequences = new Promise();
thinking to call .resolve(_receivers...)
within the _fetch..
function here and there but I highly doubt it will work and will be easy to debug.
What would be the correct/elegant way to achieve this?