classtranscribe / FrontEnd

The React + Redux Frontend for ClassTranscribe
https://classtranscribe.illinois.edu
Other
25 stars 27 forks source link

Convert awaits inside loops to Promise.all #735

Closed angrave closed 6 months ago

angrave commented 7 months ago

awaits inside a loop should be rewritten using Promise.all. (ChatGPT can give examples). Here's one example.. Search the codebase for Promise.all for other examples.

// First create an array of promises...
  const stuff_to_do = uploadedMedias.map(async (media, i) => {
    try {
      if( await this.uploadMayThrowException(playlistId, media, onUploadProgress) === OKAY) return i+1;
      throw new Error("Yikes");
    } catch (error) {
      console.error(`Error uploading media at index ${i}: ${error.message}`);
      return -(i+1);
    }
  });
// now await on them all
 const results = await Promise.all(stuff_to_do);
 const uploadedVideoIndex = results.filter((index) => index > 0); // todo subtract one.

lint found the following -

src/components/CTEPubListScreen/index.js
  71:9  warning  Unexpected `await` inside a loop  no-await-in-loop

src/screens/EPub/controllers/file-builders/HTMLFileBuilder.js
  170:24  warning  Unexpected `await` inside a loop  no-await-in-loop
  278:26  warning  Unexpected `await` inside a loop  no-await-in-loop

src/screens/Instructor/InstPlaylist/components/MediaList/UploadFile/index.js
  36:23  warning  Unexpected `await` inside a loop  no-await-in-loop

src/screens/Instructor/InstPlaylist/components/UploadFiles/index.js
  39:23  warning  Unexpected `await` inside a loop  no-await-in-loop
angrave commented 6 months ago

Reviewed and fixed