Last active
August 18, 2017 15:00
-
-
Save gitfaf/ab19a504d953a01628e096a7a2c36850 to your computer and use it in GitHub Desktop.
Retry until you have a value.
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
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