async-await.js 5.1 KB

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