Amend downloadFiles loop to a Promise.all to ensure writes are complete#6863
Draft
EvilGenius13 wants to merge 1 commit intomainfrom
Draft
Amend downloadFiles loop to a Promise.all to ensure writes are complete#6863EvilGenius13 wants to merge 1 commit intomainfrom
EvilGenius13 wants to merge 1 commit intomainfrom
Conversation
Contributor
Coverage report
Test suite run success3777 tests passing in 1455 suites. Report generated by 🧪jest coverage report action from efa913d |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
WHY are these changes introduced?
The current forEach loop in
downloadFiles()doesn't await async callbacks. This causes the function to return immediately while file writes are still in progress. The CLI will show 100% progress even though files may still be writing to disk, and if there are any errors, they won't surface since the function has already completed.WHAT is this pull request doing?
Replaces
forEach(async ...)withPromise.all()to properly await all file writes. If there is an error, it will properly propagate up.How to test your changes?
theme pulland ensure downloads still work and the CLI terminal output works.You can also run this handy script to see how
forEachdoesn't await in the loop.Details
``` const POKEMON_IDS = [1, 4, 7, 25, 133];async function fetchPokemon(id) {
const response = await fetch(
https://pokeapi.co/api/v2/pokemon/${id});const data = await response.json();
return data.name;
}
function timestamp() {
return
[${new Date().toISOString().split("T")[1].slice(0, -1)}];}
async function withForEach() {
console.log(
\n--- forEach ---);console.log(
${timestamp()} forEach started);POKEMON_IDS.forEach(async (id) => {
const name = await fetchPokemon(id);
console.log(
${timestamp()} fetched: ${name});});
console.log(
${timestamp()} forEach complete\n);}
async function withPromiseAll() {
console.log(
\n--- Promise.all ---);console.log(
${timestamp()} Promise.all started);await Promise.all(
POKEMON_IDS.map(async (id) => {
const name = await fetchPokemon(id);
console.log(
${timestamp()} fetched: ${name});})
);
console.log(
${timestamp()} Promise.all complete\n);}
async function main() {
await withForEach();
// give the orphaned forEach fetches time to print
await new Promise((resolve) => setTimeout(resolve, 3000));
await withPromiseAll();
}
main().catch(console.error);