This approach works fine if you don't care about any of the results of the promises, but not if you need to use those results (for example, if they're API responses):
Instead, you can store the promises to an intermediate variable, then loop over it with forEach for the side-effectey stuff, meanwhile awaiting it with Promise.all so you can do something with the result:
You could further abstract the loading bar logic something like this:
constwithLoadingBar=(promises,renderFn,doneMessage)=>{letcount=0promises.forEach(promise=>promise.then(()=>renderFn(`Loading ${++count} of ${promises.length}`)))constall=Promise.all(promises)all.then(()=>renderFn(doneMessage))returnall}(async()=>{constmax=5constpromises=[...newArray(max)].map((_,i)=>resolveToVal(i))constresult=awaitwithLoadingBar(promises,console.warn,'Done!')console.log(result)})()
This approach works fine if you don't care about any of the results of the promises, but not if you need to use those results (for example, if they're API responses):
Instead, you can store the promises to an intermediate variable, then loop over it with
forEach
for the side-effectey stuff, meanwhileawait
ing it withPromise.all
so you can do something with the result:You could further abstract the loading bar logic something like this:
Too complex.
Nice! I came up with something Promise-agnostic, although a little more cumbersome to use because of that:
Used something like this:
Thank you OP for the article and video, of course. 🙏