Created
September 2, 2020 01:41
-
-
Save petsel/bfab76adedfbef95665cc700daa26f60 to your computer and use it in GitHub Desktop.
implementation of a possible static `Function.afterFinallyAll` which does run a list of whatever functions failsafe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see [https://stackoverflow.com/questions/51158888/how-to-use-common-try-catch-for-processing-every-given-function-in-javascript/63690100#63690100] | |
function first(...args) { | |
console.log("first :: does succeed :: argsList :", args); | |
return args.join(', '); | |
} | |
function second(...args) { | |
console.log("second :: going to fail :: argsList :", args); | |
throw new Error('2nd invocation failed.'); | |
} | |
function third(...args) { | |
console.log("third :: going to fail :: argsList :", args); | |
throw new Error('3rd invocation failed.'); | |
} | |
function fourth(...args) { | |
console.log("fourth :: does succeed :: argsList :", args); | |
return args.join(', '); | |
} | |
function fifth(...args) { | |
console.log("fifth :: does succeed :: argsList :", args); | |
return args.join(', '); | |
} | |
const descriptorList = [{ | |
method: first, | |
argsArray: ['foo', 'bar'] | |
}, { | |
method: second, | |
argsArray: ['baz', 'biz'] | |
}, { | |
method: third, | |
argsArray: ['buz', 'foo'] | |
}, { | |
method: fourth, | |
argsArray: ['bar', 'baz'] | |
}, { | |
method: fourth, | |
argsArray: ['biz', 'buz'] | |
}]; | |
console.log( | |
'Function.afterFinallyAll(descriptorList) : ', | |
Function.afterFinallyAll(descriptorList) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see [https://gist.github.com/petsel/3b31cd5e63d43b9e7c4e#file-function-afterfinally-js] | |
(function (Function) { | |
const fctPrototype = Function.prototype; | |
const FUNCTION_TYPE = (typeof Function); | |
function isFunction(type) { | |
return ( | |
(typeof type == FUNCTION_TYPE) | |
&& (typeof type.call == FUNCTION_TYPE) | |
&& (typeof type.apply == FUNCTION_TYPE) | |
); | |
} | |
function getSanitizedTarget(target) { | |
/* eslint-disable */ | |
/* jshint ignore:start */ | |
/* ignore jslint start */ | |
/* no-eq-null */ | |
return ((target != null) && target) || null; | |
/* ignore jslint end */ | |
/* jshint ignore:end */ | |
/* eslint-enable */ | |
} | |
function afterFinally/*Modifier*/(handler, target) { | |
target = getSanitizedTarget(target); | |
const proceed = this; | |
return ( | |
isFunction(handler) && | |
isFunction(proceed) && | |
function () { | |
const context = target || getSanitizedTarget(this); | |
const args = arguments; | |
let result, error; | |
try { | |
result = proceed.apply(context, args); | |
} catch (exception) { | |
error = exception; | |
} // finally { ... } | |
result = (error || result); | |
handler.call(context, result, args); | |
return result; | |
} | |
) || proceed; | |
} | |
// afterFinally.toString = () => 'afterFinally() { [native code] }'; | |
Object.defineProperty(fctPrototype, 'afterFinally', { | |
configurable: true, | |
writable: true, | |
value: afterFinally/*Modifier*/ | |
}); | |
// provide static implementation as well. | |
function staticAfterFinally/*Modifier*/(proceed, handler, target) { | |
return afterFinally.call(proceed, handler, target); | |
} | |
// staticAfterFinally.toString = () => 'afterFinally() { [native code] }'; | |
// staticAfterFinally.toString = () => 'staticAfterFinally() { [native code] }'; | |
Object.defineProperty(Function, 'afterFinally', { | |
configurable: true, | |
writable: true, | |
value: staticAfterFinally/*Modifier*/ | |
}); | |
}(Function)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// see [https://gist.github.com/petsel/3b31cd5e63d43b9e7c4e#file-function-afterfinally-js] | |
// | |
// const staticAfterFinally/*Modifier*/ = Function.afterFinally; | |
(function (Function, Array) { | |
const isArray = Array.isArray; | |
const arrayFrom = Array.from; | |
function getSanitizedArgsArray(argsArray) { | |
/* eslint-disable */ | |
/* jshint ignore:start */ | |
/* ignore jslint start */ | |
/* no-eq-null */ | |
return ((argsArray != null) && Array.from(argsArray)) || []; | |
/* ignore jslint end */ | |
/* jshint ignore:end */ | |
/* eslint-enable */ | |
} | |
const FUNCTION_TYPE = (typeof Function); | |
function isFunction(type) { | |
return ( | |
(typeof type == FUNCTION_TYPE) | |
&& (typeof type.call == FUNCTION_TYPE) | |
&& (typeof type.apply == FUNCTION_TYPE) | |
); | |
} | |
/** | |
* map functionality which returns the result of invoking a method via its descriptor. | |
* | |
* - an `afterFinally` modified function does always return either the result of the | |
* original function's/method's invocation or the try-catch exception of the | |
* invocation attempt. | |
*/ | |
function getInvocationResult(descriptor) { | |
return (descriptor.method // - modify original function/method towards an | |
.afterFinally(() => null) // `afterFinally` handling of its try-catch result. | |
.apply( | |
descriptor.target, | |
getSanitizedArgsArray(descriptor.argsArray) | |
) | |
); | |
} | |
function afterFinallyAll(descriptorList) { | |
let result; | |
if ( | |
isArray(descriptorList) | |
&& descriptorList.every(descriptor => isFunction(descriptor.method)) | |
) { | |
result = descriptorList.map(getInvocationResult); | |
} | |
return result; | |
} | |
Object.defineProperty(Function, 'afterFinallyAll', { | |
configurable: true, | |
writable: true, | |
value: afterFinallyAll | |
}); | |
}(Function, Array, Object)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment