From 101b994c5683b9e55e6438e67e7bcc2e6399684e Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sat, 2 Jul 2022 20:45:52 +0200 Subject: [PATCH] Fix chained promise resolves not correctly passing value when then is empty --- src/LuaPrinter.ts | 1 + src/lualib/Promise.ts | 5 ++++- test/unit/builtins/promise.spec.ts | 30 +++++++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/LuaPrinter.ts b/src/LuaPrinter.ts index ed6163dc0..62fda173a 100644 --- a/src/LuaPrinter.ts +++ b/src/LuaPrinter.ts @@ -241,6 +241,7 @@ export class LuaPrinter { // Inline lualib features sourceChunks.push("-- Lua Library inline imports\n"); sourceChunks.push(loadInlineLualibFeatures(file.luaLibFeatures, this.emitHost)); + sourceChunks.push("-- End of Lua Library inline imports\n"); } if (this.options.sourceMapTraceback && !isBundleEnabled(this.options)) { diff --git a/src/lualib/Promise.ts b/src/lualib/Promise.ts index 99f144c0e..b55330713 100644 --- a/src/lualib/Promise.ts +++ b/src/lualib/Promise.ts @@ -85,7 +85,7 @@ export class __TS__Promise implements Promise { } } else { // We always want to resolve our child promise if this promise is resolved, even if we have no handler - this.fulfilledCallbacks.push(() => resolve(undefined)); + this.fulfilledCallbacks.push(v => resolve(v)); } if (onRejected) { @@ -96,6 +96,9 @@ export class __TS__Promise implements Promise { // If promise already rejected, immediately call callback internalCallback(this.rejectionReason); } + } else { + // We always want to reject our child promise if this promise is rejected, even if we have no handler + this.rejectedCallbacks.push(err => reject(err)); } if (isFulfilled) { diff --git a/test/unit/builtins/promise.spec.ts b/test/unit/builtins/promise.spec.ts index 10a0c767a..fa6f6cf0f 100644 --- a/test/unit/builtins/promise.spec.ts +++ b/test/unit/builtins/promise.spec.ts @@ -523,6 +523,34 @@ test("chained then throws", () => { ]); }); +test("empty then resolves", () => { + util.testFunction` + const { promise, resolve } = defer(); + + promise.then().then(v => { log("then2", v) }); + + resolve("mydata"); + + return allLogs; + ` + .setTsHeader(promiseTestLib) + .expectToEqual(["then2", "mydata"]); +}); + +test("empty then rejects", () => { + util.testFunction` + const { promise, reject } = defer(); + + promise.then().catch(err => { log("catch", err) }); + + reject("my error"); + + return allLogs; + ` + .setTsHeader(promiseTestLib) + .expectToEqual(["catch", "my error"]); +}); + test("catch on rejected promise immediately calls callback", () => { util.testFunction` Promise.reject("already rejected").catch(reason => { log(reason); }); @@ -592,7 +620,7 @@ describe("finally behaves same as then/catch", () => { log("final code"); }) .catch(reason => { - log("handling error", data); + log("handling error", reason); log("final code"); }); `;