Skip to content

Instantly share code, notes, and snippets.

@yitonghe00
Created July 30, 2021 00:34
Show Gist options
  • Save yitonghe00/7c2b72ac633f0a9f0f9ee74c81d002fb to your computer and use it in GitHub Desktop.
Save yitonghe00/7c2b72ac633f0a9f0f9ee74c81d002fb to your computer and use it in GitHub Desktop.
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