mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:07:35 +00:00
LibJS: Implement await properly for async functions
Fixes #20275 ``` Summary: Diff Tests: +4 ✅ -4 ❌ Diff Tests: test/built-ins/Array/fromAsync/non-iterable-input-with-thenable -async-mapped-awaits-callback-result-once.js ❌ -> ✅ test/language/expressions/await/async-await-interleaved.js ❌ -> ✅ test/language/expressions/await/await-awaits-thenables-that- throw.js ❌ -> ✅ test/language/expressions/await/await-awaits-thenables.js ❌ -> ✅ ```
This commit is contained in:
parent
5003b1a421
commit
ae7a0c43a9
5 changed files with 175 additions and 69 deletions
|
@ -7,7 +7,7 @@ describe("correct behaviour", () => {
|
|||
yield b + 1;
|
||||
yield Promise.resolve(b + 2);
|
||||
yield* [Promise.resolve(b + 3), Promise.resolve(b + 4), Promise.resolve(b + 5)];
|
||||
return b + 6;
|
||||
return Promise.resolve(b + 6);
|
||||
}
|
||||
|
||||
test("length is 1", () => {
|
||||
|
@ -64,7 +64,7 @@ describe("correct behaviour", () => {
|
|||
expect(seventhRunResult).toBe(9);
|
||||
});
|
||||
|
||||
test("can return a value", () => {
|
||||
test("can return a value and return implicitly awaits", () => {
|
||||
const eighthRunResult = runGenerator("bad7", false);
|
||||
expect(eighthRunResult.value).toBe(10);
|
||||
expect(eighthRunResult.done).toBeTrue();
|
||||
|
|
|
@ -202,7 +202,7 @@ describe("await cannot be used in class static init blocks", () => {
|
|||
});
|
||||
|
||||
describe("await thenables", () => {
|
||||
test.xfail("async returning a thanable variable without fulfilling", () => {
|
||||
test("async returning a thanable variable without fulfilling", () => {
|
||||
let isCalled = false;
|
||||
const obj = {
|
||||
then() {
|
||||
|
@ -216,7 +216,7 @@ describe("await thenables", () => {
|
|||
expect(isCalled).toBe(true);
|
||||
});
|
||||
|
||||
test.xfail("async returning a thanable variable that fulfills", () => {
|
||||
test("async returning a thanable variable that fulfills", () => {
|
||||
let isCalled = false;
|
||||
const obj = {
|
||||
then(fulfill) {
|
||||
|
@ -231,7 +231,7 @@ describe("await thenables", () => {
|
|||
expect(isCalled).toBe(true);
|
||||
});
|
||||
|
||||
test.xfail("async returning a thenable directly without fulfilling", () => {
|
||||
test("async returning a thenable directly without fulfilling", () => {
|
||||
let isCalled = false;
|
||||
const f = async () => ({
|
||||
then() {
|
||||
|
@ -243,7 +243,7 @@ describe("await thenables", () => {
|
|||
expect(isCalled).toBe(true);
|
||||
});
|
||||
|
||||
test.xfail("async returning a thenable directly that fulfills", () => {
|
||||
test("async returning a thenable directly that fulfills", () => {
|
||||
let isCalled = false;
|
||||
const f = async () => ({
|
||||
then(fulfill) {
|
||||
|
@ -256,3 +256,39 @@ describe("await thenables", () => {
|
|||
expect(isCalled).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("await observably looks up constructor of Promise objects", () => {
|
||||
let calls = 0;
|
||||
function makeConstructorObservable(promise) {
|
||||
Object.defineProperty(promise, "constructor", {
|
||||
get() {
|
||||
calls++;
|
||||
return Promise;
|
||||
},
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
async function test() {
|
||||
await makeConstructorObservable(Promise.resolve(1));
|
||||
await makeConstructorObservable(
|
||||
new Promise(resolve => {
|
||||
resolve();
|
||||
})
|
||||
);
|
||||
await makeConstructorObservable(new Boolean(true));
|
||||
await makeConstructorObservable({});
|
||||
await makeConstructorObservable(new Number(2));
|
||||
try {
|
||||
await makeConstructorObservable(Promise.reject(3));
|
||||
} catch {}
|
||||
try {
|
||||
return makeConstructorObservable(Promise.reject(1));
|
||||
} catch {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
test();
|
||||
runQueuedPromiseJobs();
|
||||
expect(calls).toBe(4);
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue