asyncfunctiontryAll(promises,name){constresolved=[];constrejected=[];// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettledconstall=awaitPromise.allSettled(promises);for(constoutcomeofall)if(outcome.hasOwnProperty('value'))resolved.push(outcome.value);elserejected.push(outcome.reason);if(rejected.length<1)returnresolved;consterror=newError('tryAll encountered errors');error.name=name;error.rejected=rejected;throwerror;}constERROR_NAME_MULTI_REJECT='ErrorMultiReject';functionhandleMultiReject(multiError){for(consterrofmultiError.rejected)console.log('Multi Error',err.toString());}functionrouteError(err){if(err.name===ERROR_NAME_MULTI_REJECT)handleMultiReject(err);elseconsole.log('Other Error -',err.toString());}functionmakeError(source,name){consterror=newError(`Error from ${source}`);error.name=name;returnerror;}consttestFn=(result,source,name)=>result?Promise.resolve(result):Promise.reject(makeError(source,name));asyncfunctionmain(){try{constone=awaittestFn('one',`testFnOne`,'ErrorOne');constresults=awaittryAll([testFn(null,`testFnTwo`,'ErrorTwo'),testFn(null,`testFnThree`,'ErrorThree'),testFn('four',`testFnFour`,'ErrorFour'),],ERROR_NAME_MULTI_REJECT);console.log('done');}catch(err){routeError(err);}}main();// "Multi Error", "ErrorTwo: Error from testFnTwo"// "Multi Error", "ErrorThree: Error from testFnThree"
Perhaps you meant something else - in which case chaining the error handlers may be worth considering:
// wrap the error with one that is uniquely named ...asyncfunctionwrapError(promise,name){try{// https://jakearchibald.com/2017/await-vs-return-vs-return-await/#return-awaitingreturnawaitpromise;}catch(err){consterror=newError('Wrapped Error');error.name=name;error.wrapped=err;throwerror;}}functionmakeChainedHandler(next,handler){returnnext?(err)=>{handler(err);next(null);}:handler;}// ... so that the appropriate error handler// to process the wrapped error can be retrieved// which calls any other chained "aborted" handlers. //functionhandleError(err){if(err.hasOwnProperty('wrapped')){consthandler=handlers.get(err.name);if(handler){handler(err.wrapped);return;}err=err.wrapped;}console.log('Other Error -',err.toString());}asyncfunctionmain(){try{constone=awaitwrapError(demoFnOne('one'),ERROR_NAME_ONE);consttwo=awaitwrapError(demoFnTwo(null),ERROR_NAME_TWO);constthree=awaitwrapError(demoFnThree('three'),ERROR_NAME_THREE);constfour=awaitwrapError(demoFnFour('four'),ERROR_NAME_FOUR);console.log('done');}catch(err){handleError(err);}}// --- Begin Demo SupportconstDEMO_ONE='DemoFnOne';constDEMO_TWO='DemoFnTwo';constDEMO_THREE='DemoFnThree';constDEMO_FOUR='DemoFnFour';constERROR_NAME_ONE='ErrorOne';constERROR_NAME_TWO='ErrorTwo';constERROR_NAME_THREE='ErrorThree';constERROR_NAME_FOUR='ErrorFour';constconfig=[[DEMO_ONE,ERROR_NAME_ONE],[DEMO_TWO,ERROR_NAME_TWO],[DEMO_THREE,ERROR_NAME_THREE],[DEMO_FOUR,ERROR_NAME_FOUR],];functionmakeError(fnName){consterror=newError(`Error from ${fnName}`);error.name='Error'+fnName[0].toUpperCase()+fnName.slice(1);returnerror;}constmakeDemoFn=(fnName)=>(result)=>result?Promise.resolve(result):Promise.reject(makeError(fnName));functionmakeErrorHandler(fnName){return(err)=>{if(err){console.log(err.toString());return;}console.log(`Error: ${fnName} was aborted.`);};}functionmakeBoth([fnName,errName]){constfn=makeDemoFn(fnName);consthandler=makeErrorHandler(fnName);return[[fnName,fn],[errName,handler],];}const[fns,handlers]=(()=>{constfnEntries=[];consthandlerEntries=[];letchained=null;// in reverse to set up the necessary abort chainingfor(leti=config.length-1;i>=0;i-=1){const[fnEntry,[name,handler]]=makeBoth(config[i]);chained=makeChainedHandler(chained,handler);fnEntries.push(fnEntry);handlerEntries.push([name,chained]);}return[newMap(fnEntries),newMap(handlerEntries)];})();constdemoFnOne=fns.get(DEMO_ONE);constdemoFnTwo=fns.get(DEMO_TWO);constdemoFnThree=fns.get(DEMO_THREE);constdemoFnFour=fns.get(DEMO_FOUR);// --- End Demo Supportmain();// "ErrorDemoFnTwo: Error from DemoFnTwo"// "Error: DemoFnThree was aborted."// "Error: DemoFnFour was aborted."
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.
But what if we have all the promise dependend on promise above them, and we want to handle error for each of them?
Perhaps you meant something else - in which case chaining the error handlers may be worth considering: