I'm currently facing an issue with handling promises in my code - specifically, the Promises returned are not correctly mapped to the utteranceId as desired. I want to ensure that the ids are preserved for the data being returned. Even after using Object.entries(utteranceObject)
to wrap my object.
When I log keyValue
after applying
Object.entries(utteranceObject).map(keyValue)
, the data appears to be formatted correctly as [id, url]
. How should it be formatted when passing to Promise.all()
?
const getUtterancesAudio = async (
utterances: { utteranceId: string, interviewId: string }[]): Promise<void> => {
const config = useRuntimeConfig();
const mapped = utterances.map((u) => {
return { id: u.utteranceId, url: `${config.public.apiBase}/utterance/182bd8fe-f57d-
4595-b8d7-649ff8ca6d62/interview/665a799a-883b-4b20-9460-3eee486516a8/audio` };
});
const flatten = mapped.map(Object.values);
const forPromisesEntries = Object.fromEntries(flatten);
const getResults = await createPromises(forPromisesEntries);
};
const createPromises = async (utteranceObject: Record<string, string>):
Promise<unknown> => {
const promises = Object.entries(utteranceObject).map(keyValue =>
buildAudioFetchRequests(keyValue[0], keyValue[1]));
return Promise.all(promises);
};
const buildAudioFetchRequests = async (key: string, url: string): Promise<unknown> =>
{
return [key, useAuthenticatedFetch(url, { initialCache: false }]);
};