Guest User

Untitled

a guest
Mar 24th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. class MultiplicatorUnitFailure extends Error {}
  2.  
  3. function primitiveMultiply(a, b) {
  4. if (Math.random() < 0.2) {
  5. return a * b;
  6. } else {
  7. throw new MultiplicatorUnitFailure("Klunk");
  8. }
  9. }
  10.  
  11. function reliableMultiply(a, b) {
  12. try {
  13. return primitiveMultiply(a, b);
  14. } catch (e) {
  15. if (e instanceof MultiplicatorUnitFailure) {
  16. return reliableMultiply(a, b);
  17. } else {
  18. throw e;
  19. }
  20. }
  21. }
  22.  
  23. console.log(reliableMultiply(8, 8));
  24. // → 64
Add Comment
Please, Sign In to add comment