Created
July 30, 2021 00:34
-
-
Save yitonghe00/7c2b72ac633f0a9f0f9ee74c81d002fb to your computer and use it in GitHub Desktop.
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
class MultiplicatorUnitFailure extends Error {} | |
function primitiveMultiply(a, b) { | |
if (Math.random() < 0.2) { | |
return a * b; | |
} else { | |
throw new MultiplicatorUnitFailure("Klunk"); | |
} | |
} | |
function reliableMultiply(a, b) { | |
// Your code here. | |
let ret; | |
while(!ret) { | |
try { | |
ret = primitiveMultiply(a, b); | |
} catch (error) { | |
if (error instanceof MultiplicatorUnitFailure) { | |
console.log('Failed to get the result'); | |
} else { | |
throw error; | |
} | |
} | |
} | |
return ret; | |
} | |
console.log(reliableMultiply(8, 8)); | |
// → 64 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment