Skip to content

Commit

Permalink
fix: returning binding from awaited fn calling .then (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-elicx authored Aug 31, 2023
1 parent c330ca5 commit ab73898
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/twenty-pots-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'cf-bindings-proxy': patch
---

Fixes returning a binding through an awaited function calling `.then` on `binding(...)` and throwing an error.
2 changes: 2 additions & 0 deletions src/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ export const createBindingProxy = <T>(bindingId: string, notChainable = false):
// ignore toJSON calls
if (prop === 'toJSON') return undefined;
if (notChainable) return undefined;
// ignore then calls if there are no calls yet
if (target.__calls.length === 0 && prop === 'then') return undefined;

// decide if we should chain until a certain point for this call
if (!target.__chainUntil.length) {
Expand Down
6 changes: 6 additions & 0 deletions tests/proxy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,5 +411,11 @@ suite('bindings', () => {
// @ts-expect-error - testing it doesn't throw an error, not that it works
expect(kv[Symbol.toStringTag]).toBeDefined();
});

test('Returning binding from awaited function should not try to call `then`', async () => {
const getBinding = async () => binding<KVNamespace>('KV');
const kv = await getBinding();
expect(kv).toBeDefined();
});
});
});

0 comments on commit ab73898

Please sign in to comment.