Advertisement
Guest User

JS

a guest
Jul 24th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // RestClient.js
  2.  
  3.   async getData() {
  4.    
  5.     const response = await fetch(url);
  6.  
  7.     if (response.status != 200) {
  8.       throw "Server error (" + response.status + ")";
  9.     }
  10.  
  11.     const data = await response.json();
  12.  
  13.     if (data.error) {
  14.       throw data.error;
  15.     }
  16.  
  17.     return data;
  18.   }
  19.  
  20. // Outside class
  21.   async myMethod() {
  22.     try {
  23.       const data = await restClient.getData();
  24.     }
  25.     catch (e) {
  26.       console.log('ERROR: ', e)
  27.     }
  28.    
  29.     console.log('Data recieved:', data);
  30.   }
  31.  
  32.   otherMethod() {
  33.     myMethod();
  34.     console.log(this.causes.an.error); // This error will be caught by the catch block from myMethod
  35.    
  36.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement