9 ms·
So, thunks should avoid multiple evaluation, too, and handle exceptions in a way that is relatively coherent. I like: function thunk(f) { var evaluated
by hawkice 11y ago
So, thunks should avoid multiple evaluation, too, and handle exceptions in a way that is relatively coherent. I like:
function thunk(f) {
var evaluated = false;
var value = undefined;
var excepting = undefined;
return { force: function () {
if (!evaluated) {
evaluated = true;
try { value = f(); } catch (e) { excepting = e; }
}
if (excepting) throw excepting;
return value;
}}
}