Skip to content

Commit f719eb8

Browse files
committed
Polyfill Promise.withResolvers().
This hopefully improves compatibility with old Safari versions and Hermes (React Native). Unfortunately it didn't seem easy to extend the tests to cover React Native. There is a vitest-react-native package, but it looks little-used and unmaintained, so I didn't want to install it. Might help with #91, though I won't declare that one "fixed" until we actually have tests proving it.
1 parent a3202ad commit f719eb8

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

.changeset/six-banks-pull.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"capnweb": patch
3+
---
4+
5+
Polyfilled Promise.withResolvers() to improve compatibility with old Safari versions and Hermes (React Native).

src/core.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,20 @@ if (!Symbol.asyncDispose) {
1313
(Symbol as any).asyncDispose = Symbol.for('asyncDispose');
1414
}
1515

16+
// Polyfill Promise.withResolvers() for old Safari versions (ugh), Hermes (React Native), and
17+
// maybe others.
18+
if (!Promise.withResolvers) {
19+
Promise.withResolvers = function<T>(): PromiseWithResolvers<T> {
20+
let resolve: (value: T | PromiseLike<T>) => void;
21+
let reject: (reason?: any) => void;
22+
const promise = new Promise<T>((res, rej) => {
23+
resolve = res;
24+
reject = rej;
25+
});
26+
return { promise, resolve: resolve!, reject: reject! };
27+
};
28+
}
29+
1630
let workersModule: any = (globalThis as any)[WORKERS_MODULE_SYMBOL];
1731

1832
export interface RpcTarget {

0 commit comments

Comments
 (0)