Untitled

By Flying Marmoset, 3 Weeks ago, written in Plain Text.
URL https://paste.tinyw.in/index.php/view/85167119
Download Paste or View RawExpand paste to full width of browser | Change Viewing Options
  1. function doWebRequest(){
  2.   return new Promise(function(resolve, reject){
  3.     //some code that takes a few seconds asynchronously
  4.     if(somethingErrored){
  5.       return reject(new Error("oh noes"));
  6.     }else{
  7.       return resolve("SUCCESS!");
  8.     }
  9.   });
  10. }
  11.  
  12. async function doWebRequestAsync(){
  13.   try{
  14.     //some code that takes a few seconds asynchronously (must use await, no callbacks allowed)
  15.     return "SUCCESS!";
  16.   }catch(e){
  17.     throw new Error("oh noes");
  18.   }
  19. }
  20.  
  21.  
  22. //This is valid because they both return a promise
  23. (async () => {
  24.   try{
  25.     console.log(await doWebRequest()); //Prints success!
  26.     console.log(await doWebRequestAsync()); // Prints success!
  27.   }catch(e){
  28.     console.log(e); //Prints oh noes
  29.   }
  30. })();
  31.  
  32. doWebRequest().then(function(result){
  33.   console.log(result); // Prints success
  34. }).catch(function(e){
  35.   console.log(e); //prints oh noes
  36. });
  37.  
  38. doWebRequestAsync().then(function(result){
  39.   console.log(result); // Prints success
  40. }).catch(function(e){
  41.   console.log(e); //prints oh noes
  42. });

Reply to "Untitled"

Here you can reply to the paste above

Use TinyW.in

Make Private

Feeling clever? Set some advanced options.