Next.js Discord

Discord Forum

Error with Async Await

Answered
Bald Eagle posted this in #help-forum
Open in Discord
Bald EagleOP
Long story short, I need to do an api call to get a file for a list of items. Thus, I need to do n async calls. However, I have no idea how to do that.

This is my current code
let audioMap = new Map<string, Blob | undefined>()
audioMap = new Map(audioItems.map( await (audioItem : AudioData) =>
  [audioItem.audio_file_id, await getAudioFile(audioItem.audio_file_id)]
))


This is the Audio Data type:
export type AudioData = {
  id: string;
  created_at: string;
  transcript: string;
  summary: string;
  audio_file_id: string;
  user_id: string;
}


This is the getAudioFile function:
async function getAudioFile(audioFileId: string) {

    const {data, error} = await supabase.storage.from("audio").download(user?.id + "/" + audioFileId)
    if (data) {
        return data
    } else {
        console.log(error)
    }
  }


How do I reformat my code so that I can get a mapping of <audio_file_id, Blob> without a Promise?
Answered by DirtyCajunRice | AppDir
you should await Promise.all() the map of async requests, then reduce to your map.
View full answer

2 Replies

you should await Promise.all() the map of async requests, then reduce to your map.
Answer
Bald EagleOP
Thank you!