async-await.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. describe("parsing freestanding async functions", () => {
  2. test("simple", () => {
  3. expect(`async function foo() {}`).toEval();
  4. expect(`async
  5. function foo() {}`).not.toEval();
  6. });
  7. test("await expression", () => {
  8. expect(`async function foo() { await bar(); }`).toEval();
  9. expect(`async function foo() { await; }`).not.toEval();
  10. expect(`function foo() { await bar(); }`).not.toEval();
  11. expect(`function foo() { await; }`).toEval();
  12. expect(`\\u0061sync function foo() { await bar(); }`).not.toEval();
  13. expect(`\\u0061sync function foo() { \\u0061wait bar(); }`).not.toEval();
  14. expect(`async function foo() { \\u0061wait bar(); }`).not.toEval();
  15. });
  16. });
  17. describe("parsing object literal async functions", () => {
  18. test("simple", () => {
  19. expect(`x = { async foo() { } }`).toEval();
  20. expect(`x = { async
  21. foo() { } }`).not.toEval();
  22. });
  23. test("property on object called async", () => {
  24. expect(`x = { async() { } }`).toEval();
  25. expect(`x = { async() { await 4; } }`).not.toEval();
  26. expect(`x = { async: 3 }`).toEval();
  27. expect(`x = { async: await 3, }`).not.toEval();
  28. });
  29. test("await expression", () => {
  30. expect(`x = { foo() { await bar(); } }`).not.toEval();
  31. expect(`x = { foo() { await; } }`).toEval();
  32. expect(`x = { async foo() { await bar(); } }`).toEval();
  33. expect(`x = { async foo() { await; } }`).not.toEval();
  34. });
  35. });
  36. describe("parsing classes with async methods", () => {
  37. test("simple", () => {
  38. expect(`class Foo { async foo() {} }`).toEval();
  39. expect(`class Foo { static async foo() {} }`).toEval();
  40. expect(`class Foo { async foo() { await bar(); } }`).toEval();
  41. expect(`class Foo { async foo() { await; } }`).not.toEval();
  42. expect(`class Foo { async constructor() {} }`).not.toEval();
  43. });
  44. });
  45. test("function expression names equal to 'await'", () => {
  46. expect(`async function foo() { (function await() {}); }`).toEval();
  47. expect(`async function foo() { function await() {} }`).not.toEval();
  48. });
  49. test("async function cannot use await in default parameters", () => {
  50. expect("async function foo(x = await 3) {}").not.toEval();
  51. expect("async function foo(x = await 3) {}").not.toEval();
  52. // Even as a reference to some variable it is not allowed
  53. expect(`
  54. var await = 4;
  55. async function foo(x = await) {}
  56. `).not.toEval();
  57. });
  58. describe("async arrow functions", () => {
  59. test("basic syntax", () => {
  60. expect("async () => await 3;").toEval();
  61. expect("async param => await param();").toEval();
  62. expect("async (param) => await param();").toEval();
  63. expect("async (a, b) => await a();").toEval();
  64. expect("async () => { await 3; }").toEval();
  65. expect("async param => { await param(); }").toEval();
  66. expect("async (param) => { await param(); }").toEval();
  67. expect("async (a, b) => { await a(); }").toEval();
  68. expect(`async
  69. () => await 3;`).not.toEval();
  70. expect("async async => await async()").toEval();
  71. expect("async => async").toEval();
  72. expect("async => await async()").not.toEval();
  73. expect("async (b = await) => await b;").not.toEval();
  74. expect("async (b = await 3) => await b;").not.toEval();
  75. // Cannot escape the async keyword and get an async arrow function.
  76. expect("\\u0061sync () => await 3").not.toEval();
  77. expect("for (async of => {};false;) {}").toEval();
  78. expect("for (async of []) {}").not.toEval();
  79. expect("for (\\u0061sync of []) {}").toEval();
  80. expect("for (\\u0061sync of => {};false;) {}").not.toEval();
  81. expect("for (\\u0061sync => {};false;) {}").toEval();
  82. });
  83. test("async within a for-loop", () => {
  84. let called = false;
  85. // Unfortunately we cannot really test the more horrible case above.
  86. for (
  87. const f = async of => {
  88. return of;
  89. };
  90. ;
  91. ) {
  92. expect(f(43)).toBeInstanceOf(Promise);
  93. called = true;
  94. break;
  95. }
  96. expect(called).toBeTrue();
  97. });
  98. });
  99. test("basic functionality", () => {
  100. test("simple", () => {
  101. let executionValue = null;
  102. let resultValue = null;
  103. async function foo() {
  104. executionValue = "someValue";
  105. return "otherValue";
  106. }
  107. const returnValue = foo();
  108. expect(returnValue).toBeInstanceOf(Promise);
  109. returnValue.then(result => {
  110. resultValue = result;
  111. });
  112. runQueuedPromiseJobs();
  113. expect(executionValue).toBe("someValue");
  114. expect(resultValue).toBe("otherValue");
  115. });
  116. test("await", () => {
  117. let resultValue = null;
  118. async function foo() {
  119. return "someValue";
  120. }
  121. async function bar() {
  122. resultValue = await foo();
  123. }
  124. bar();
  125. runQueuedPromiseJobs();
  126. expect(resultValue).toBe("someValue");
  127. });
  128. });
  129. describe("non async function declaration usage of async still works", () => {
  130. test("async as a function", () => {
  131. function async(value = 4) {
  132. return value;
  133. }
  134. expect(async(0)).toBe(0);
  135. // We use eval here since it otherwise cannot find the async function.
  136. const evalResult = eval("async(1)");
  137. expect(evalResult).toBe(1);
  138. });
  139. test("async as a variable", () => {
  140. let async = 3;
  141. const evalResult = eval("async >= 2");
  142. expect(evalResult).toBeTrue();
  143. });
  144. });