There is also a useful command for Promise.allSettled since with Promise.all if one promise crashes you will lose the track for all of them.
Using allSettled will give you the result even if one of them crashes. Downside is that with both approaches if you don't do some concurrency using Promise.all and Promise.allSettled will fire up many promises and will make the buffer overflow so it is not memory wise operation.
import{safeHandle}from'./safe-handle';importconcurrentfrom'p-limit';interfaceMyEntity{name:string;}constpromises:(()=>Promise<SafePromiseResult<MyEntity,Error>>)[]=[];constprocessRecord=async(entry:MyEntity)=>{/* My Async logic goes here */returnentry;};constentries=[{name:'Oh my god'}];for(constentryofentries){promises.push(()=>safeHandle(processRecord(entry)));}constmodifiedEntries:MyEntity[]=[];for(const[entry,error]ofawaitPromise.all(promises.map(concurrent(20)))){if(error){console.log('Error',error);}else{modifiedEntries.push(entry);}}console.log(modifiedEntries);
The same can be used with allSettled for node 12
When using concurrent approach the code itself will fire only 20 promises at time.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
There is also a useful command for Promise.allSettled since with Promise.all if one promise crashes you will lose the track for all of them.
Using
allSettled
will give you the result even if one of them crashes. Downside is that with both approaches if you don't do some concurrency usingPromise.all
andPromise.allSettled
will fire up many promises and will make the buffer overflow so it is not memory wise operation.The way it needs to be is
The same can be used with allSettled for node 12
When using concurrent approach the code itself will fire only 20 promises at time.