asyncfunction.js 806 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. function resolveAfter2Seconds(x) {
  2. return new Promise(resolve => {
  3. setTimeout(() => {
  4. resolve(x);
  5. }, 2000);
  6. });
  7. }
  8. /**
  9. * Add a number.
  10. * @param {number} x - The number to add.
  11. */
  12. async function add(x) {
  13. var a = resolveAfter2Seconds(20);
  14. var b = resolveAfter2Seconds(30);
  15. return x + await a + await b;
  16. }
  17. /**
  18. * Subtract a number.
  19. * @param {number} x - The number to subtract.
  20. */
  21. var subtract = async function subtract(x) {
  22. var a = resolveAfter2Seconds(20);
  23. var b = resolveAfter2Seconds(30);
  24. return await a + await b - x;
  25. }
  26. /**
  27. * Adder class.
  28. */
  29. class Adder {
  30. /**
  31. * Construct an Adder instance.
  32. */
  33. constructor() {}
  34. /**
  35. * Add a number.
  36. * @param {number} x - The number to add.
  37. */
  38. async add(x) {}
  39. }