Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. class Initialisation {
  2. constructor() {
  3. this.aFunctionThatThrowsImmediately();
  4. this.aFunctionThatThrowsLater();
  5. }
  6.  
  7. // Fake stuff to show the return values of module
  8. aFunctionThatThrowsImmediately() {
  9. console.log(this.errorCallback()); // error thrown, but platform not set
  10. }
  11. aFunctionThatThrowsLater() {
  12. setTimeout(() => {
  13. console.log(this.errorCallback()); // error found: platform-arbitrary-id
  14. }, 1000);
  15. }
  16.  
  17. async getPlatform () {
  18. return "platform-arbitrary-id";
  19. }
  20.  
  21. // Set a default no-op errorCallback (in case error is thrown)
  22. errorCallback() {
  23. return "error thrown, but platform not set"
  24. }
  25.  
  26. // Expose a setter for platform
  27. setErrorCallback(errorCallback) {
  28. this.errorCallback = errorCallback;
  29. }
  30. }
  31.  
  32. class Error {
  33. constructor(getPlatform) {
  34. this.setPlatform(getPlatform);
  35. }
  36. async setPlatform(getPlatform) {
  37. this.platform = await getPlatform();
  38. }
  39. onError() {
  40. return `error found: ${this.platform}`;
  41. }
  42. }
  43.  
  44. const initInstance = new Initialisation();
  45. const errorInstance = new Error(initInstance.getPlatform);
  46. initInstance.setErrorCallback(errorInstance.onError.bind(errorInstance));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement