Skip to content

Commit

Permalink
Fix returning the result of a promise.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasny committed Mar 8, 2023
1 parent f6ae963 commit 5ae9f3f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export default class Listener {
const source: WindowLike = (event.source as Window) || this.fallbackSource;
const targetOrigin = event.origin && event.origin !== "null" ? event.origin : "*";

Promise.resolve(result).then(() => {
source.postMessage({'@rpc': RESPONSE_TYPE, channel, id, result}, targetOrigin);
Promise.resolve(result).then(r => {
source.postMessage({'@rpc': RESPONSE_TYPE, channel, id, result: r}, targetOrigin);
});
}

Expand Down
7 changes: 6 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("simple-iframe-rpc", () => {
child = new JSDOM('').window;
client = new Listener({
add: (a, b) => a + b,
sub: (a, b) => a - b,
sub: (a, b) => Promise.resolve(a - b),
err: () => { throw new Error("Oops"); }
});
client.listen(child, "*");
Expand All @@ -41,6 +41,11 @@ describe("simple-iframe-rpc", () => {
assert.equal(result, 5);
});

it("gives a result of a promise", async () => {
const result = await rpc.sub(3, 2);
assert.equal(result, 1);
});

it("throws an error", async () => {
try {
await rpc.err();
Expand Down

0 comments on commit 5ae9f3f

Please sign in to comment.