Guest User

Untitled

a guest
Dec 12th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. // resolves second
  2. async function a(context) {
  3. const delay = 200;
  4. return new Promise((resolve) => {
  5. setTimeout(() => {
  6. console.log(context + ' ' + delay);
  7. resolve();
  8. }, delay);
  9. });
  10. }
  11.  
  12. // resolves third
  13. async function b(context) {
  14. const delay = 500;
  15. return new Promise((resolve) => {
  16. setTimeout(() => {
  17. console.log(context + ' ' + delay);
  18. resolve();
  19. }, delay);
  20. });
  21. }
  22.  
  23. // resolves first
  24. async function c(context) {
  25. const delay = 100;
  26. return new Promise((resolve) => {
  27. setTimeout(() => {
  28. console.log(context + ' ' + delay);
  29. resolve();
  30. }, delay);
  31. });
  32. }
  33.  
  34. async function awaitEx() {
  35. try {
  36. // these will print in the same order 1-2-3
  37. await a("awaiting 1");
  38. await b("awaiting 2");
  39. await c("awaiting 3");
  40. } catch (ignored) {
  41. }
  42. }
  43.  
  44. async function noAwait() {
  45. try {
  46. // these will print in the order the promises resolve
  47. a("not awaiting 1");
  48. b("not awaiting 2");
  49. c("not awaiting 3");
  50. } catch (ignored) {
  51. }
  52. }
  53.  
  54. noAwait();
  55. awaitEx();
Add Comment
Please, Sign In to add comment