mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 12:09:51 +02:00
feat: useAwait
This commit is contained in:
1 parent
42cada1811
commit
4033e8618f
6 files changed
+90
-27
No files matched your search
@@ -0,0 +1,19 @@
|
||||
import { use } from "react";
|
||||
|
||||
const cache = new Map<string, Promise<unknown>>();
|
||||
|
||||
export function useAwait<Type>(
|
||||
creator: () => Promise<Type>,
|
||||
discriminator: string
|
||||
) {
|
||||
let promise;
|
||||
|
||||
if (cache.has(discriminator)) {
|
||||
promise = cache.get(discriminator) as Promise<Type>;
|
||||
} else {
|
||||
promise = creator();
|
||||
cache.set(discriminator, promise);
|
||||
}
|
||||
|
||||
return use(promise);
|
||||
}
|
||||
Reference in new issue
Block a user