Skip to content

Instantly share code, notes, and snippets.

@gitfaf
Last active August 18, 2017 15:00
Show Gist options
  • Save gitfaf/ab19a504d953a01628e096a7a2c36850 to your computer and use it in GitHub Desktop.
Save gitfaf/ab19a504d953a01628e096a7a2c36850 to your computer and use it in GitHub Desktop.
Retry until you have a value.
function MultiplicatorUnitFailure (message) {
this.stack = (new Error()).stack;
this.message = message;
}
MultiplicatorUnitFailure.prototype = Object.create(Error.prototype);
MultiplicatorUnitFailure.prototype.name = 'MultiplicatorUnitFailure';
function primitiveMultiply (a, b) {
console.log(`a: ${a}; b: ${b}.`);
let val = a * b;
if ((Math.floor(val * 100)) % 2 === 0 ) {
return val;
} else {
throw new MultiplicatorUnitFailure('error');
}
}
function run () {
var value = null;
try {
value = primitiveMultiply(Math.random(), Math.random());
return value;
} catch (e) {
if (e instanceof MultiplicatorUnitFailure) {
console.log(e);
} else {
console.log('error');
}
} finally {
if (value === null) {
return run();
} else {
return value;
}
}
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment