Skip to content

Commit 288c1b7

Browse files
authored
Fix chained promise resolves not correctly passing value when then is empty (#1298)
1 parent 9e9c324 commit 288c1b7

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/LuaPrinter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ export class LuaPrinter {
241241
// Inline lualib features
242242
sourceChunks.push("-- Lua Library inline imports\n");
243243
sourceChunks.push(loadInlineLualibFeatures(file.luaLibFeatures, this.emitHost));
244+
sourceChunks.push("-- End of Lua Library inline imports\n");
244245
}
245246

246247
if (this.options.sourceMapTraceback && !isBundleEnabled(this.options)) {

src/lualib/Promise.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class __TS__Promise<T> implements Promise<T> {
8585
}
8686
} else {
8787
// We always want to resolve our child promise if this promise is resolved, even if we have no handler
88-
this.fulfilledCallbacks.push(() => resolve(undefined));
88+
this.fulfilledCallbacks.push(v => resolve(v));
8989
}
9090

9191
if (onRejected) {
@@ -96,6 +96,9 @@ export class __TS__Promise<T> implements Promise<T> {
9696
// If promise already rejected, immediately call callback
9797
internalCallback(this.rejectionReason);
9898
}
99+
} else {
100+
// We always want to reject our child promise if this promise is rejected, even if we have no handler
101+
this.rejectedCallbacks.push(err => reject(err));
99102
}
100103

101104
if (isFulfilled) {

test/unit/builtins/promise.spec.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,34 @@ test("chained then throws", () => {
523523
]);
524524
});
525525

526+
test("empty then resolves", () => {
527+
util.testFunction`
528+
const { promise, resolve } = defer<string>();
529+
530+
promise.then().then(v => { log("then2", v) });
531+
532+
resolve("mydata");
533+
534+
return allLogs;
535+
`
536+
.setTsHeader(promiseTestLib)
537+
.expectToEqual(["then2", "mydata"]);
538+
});
539+
540+
test("empty then rejects", () => {
541+
util.testFunction`
542+
const { promise, reject } = defer<string>();
543+
544+
promise.then().catch(err => { log("catch", err) });
545+
546+
reject("my error");
547+
548+
return allLogs;
549+
`
550+
.setTsHeader(promiseTestLib)
551+
.expectToEqual(["catch", "my error"]);
552+
});
553+
526554
test("catch on rejected promise immediately calls callback", () => {
527555
util.testFunction`
528556
Promise.reject("already rejected").catch(reason => { log(reason); });
@@ -592,7 +620,7 @@ describe("finally behaves same as then/catch", () => {
592620
log("final code");
593621
})
594622
.catch(reason => {
595-
log("handling error", data);
623+
log("handling error", reason);
596624
log("final code");
597625
});
598626
`;

0 commit comments

Comments
 (0)