Now that we've pre-downloaded one model, let's pre-download two at once instead of waiting for each one in turn.
downloadAsset() returns its promise the moment it's called, before it downloads a single byte, the same as completion() did when we fired two completions in one tick. .map() makes every downloadAsset() request up front, so every download starts together instead of one after another.
Promise.allSettled waits for all of them to finish. A rejected one doesn't stop it from reporting the ones that succeeded.
Map each asset straight to its own downloadAsset() invocation like so:
const downloads = assets.map((asset) =>
downloadAsset({
assetSrc: asset.src,
onProgress: (p) => {
process.stderr.write(`▸ [${asset.name}] ${p.percentage.toFixed(0)}%\n`);
},
})
);Once it resolves, results[i] lines up with assets[i] by index:
const results = await Promise.allSettled(downloads);
for (let i = 0; i < assets.length; i++) {
const status = results[i].status === "fulfilled" ? "OK" : "FAILED";
console.log(`▸ ${status} ${assets[i].name}`);
}Note: swap
Promise.allSettledforPromise.allhere and one failed download rejects the entire batch, hiding the status of every asset that succeeded.
Question 1 of 2
What does Promise.allSettled wait for before it resolves?
Question 2 of 2
What does results[i].status tell you?
Run your code, check your answer, or ask a question. It all shows up here.