Skip to content

Commit ab73898

Browse files
authored
fix: returning binding from awaited fn calling .then (#35)
1 parent c330ca5 commit ab73898

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

.changeset/twenty-pots-rule.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'cf-bindings-proxy': patch
3+
---
4+
5+
Fixes returning a binding through an awaited function calling `.then` on `binding(...)` and throwing an error.

src/proxy.ts

+2
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ export const createBindingProxy = <T>(bindingId: string, notChainable = false):
150150
// ignore toJSON calls
151151
if (prop === 'toJSON') return undefined;
152152
if (notChainable) return undefined;
153+
// ignore then calls if there are no calls yet
154+
if (target.__calls.length === 0 && prop === 'then') return undefined;
153155

154156
// decide if we should chain until a certain point for this call
155157
if (!target.__chainUntil.length) {

tests/proxy.spec.ts

+6
Original file line numberDiff line numberDiff line change
@@ -411,5 +411,11 @@ suite('bindings', () => {
411411
// @ts-expect-error - testing it doesn't throw an error, not that it works
412412
expect(kv[Symbol.toStringTag]).toBeDefined();
413413
});
414+
415+
test('Returning binding from awaited function should not try to call `then`', async () => {
416+
const getBinding = async () => binding<KVNamespace>('KV');
417+
const kv = await getBinding();
418+
expect(kv).toBeDefined();
419+
});
414420
});
415421
});

0 commit comments

Comments
 (0)