The Promise.all question kept me up at night, here is my attempt:
asyncfunctionpromiseAll(promises){returnnewPromise((resolve,reject)=>{constresult=[];// create an arrayconstresolver=(p)=>(val)=>{result[promises.indexOf(p)]=val;// fill it directly, I know this is suboptimal as it creates an array with holes// Object.keys helps with finding actual length (without holes) of the arrayif(Object.keys(result).length===promises.length){resolve(Array.from(result));// Array.from converts the sub-optimal array with holes to a regular array}};promises.forEach((p)=>p.then(resolver(p),reject));// reject early to match Promise.all's early rejection behaviour});}
The
Promise.all
question kept me up at night, here is my attempt:The most important thing here is:
Promise.all
Wow this is complicated, I need to focus :D