Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. var longFn = function(x) {
  2. return new Promise(function(res, rej) {
  3. if (x) {
  4. setTimeout(res(x), 1000);
  5. } else {
  6. rej("ERROR from not passing in a value to longFn");
  7. }
  8. });
  9. };
  10.  
  11. async function f(inputToAsync) {
  12. console.log("f() starting with param: ", inputToAsync);
  13. let result;
  14. try {
  15. result = await longFn(inputToAsync);
  16. } catch (err) {
  17. console.log("THROWING err: " + err);
  18. throw err;
  19. }
  20. return "RETURN VALUE FROM ASYNC function: " + inputToAsync;
  21. }
  22.  
  23. var p = f("INPUT TO ASYNC FUNCTION");
  24. console.log("1: return value of async function is ", p);
  25.  
  26. p.then(
  27. res => console.log("1: FINISHED: got back: " + res)
  28. ).catch(
  29. err => console.log("1: GOT ERROR: err")
  30. );
  31.  
  32.  
  33. var p = f(false);
  34. console.log("2: return value of async function is ", p);
  35.  
  36. p.then(
  37. res => console.log("2: FINISHED: got back: " + res)
  38. ).catch(
  39. err => console.log("2: GOT ERROR: err")
  40. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement