Skip to content

Commit f3dacbf

Browse files
Refactor
1 parent f84d27e commit f3dacbf

1 file changed

Lines changed: 15 additions & 6 deletions

File tree

‎app/routes/make/schema.tsx‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,14 @@ function usePromise<Data>(source: Promise<Data>): null | Data {
7676
return data;
7777
}
7878

79+
function useAsyncFunc<Data>(
80+
source: () => Promise<Data>,
81+
dependencies: Array<any>
82+
): null | Data {
83+
const promise = useMemo(source, dependencies);
84+
return usePromise(promise);
85+
}
86+
7987
interface AWSRegion {
8088
primary: string;
8189
secondary: string;
@@ -127,7 +135,7 @@ function* SwapiPersonSchema(): SchemaGenerator<SwapiPersonData> {
127135
};
128136
}
129137
function* SwapiPersonResource() {
130-
const rawData: unknown = yield getJSON(new URL(`https://swapi.py4e.com/api/people/1`));
138+
const rawData: unknown = yield getJSON('/api/people/1');
131139

132140
const parsedData = parseJSON(rawData, function* Schema() {
133141
const name: string = yield read.string("name");
@@ -175,11 +183,12 @@ export default function MakeRenderer() {
175183
const personData =
176184
personDataRaw != null ? parseJSON(personDataRaw, SwapiPersonSchema) : null;
177185

178-
const personPromise = useMemo(
179-
() => fetchController(new URL("https://swapi.py4e.com/"), SwapiPersonResource),
180-
[]
181-
);
182-
const personData2 = usePromise(personPromise);
186+
const personData2 = useAsyncFunc(() =>
187+
fetchController(
188+
new URL("https://swapi.py4e.com/"),
189+
SwapiPersonResource
190+
),
191+
[]);
183192

184193
return (
185194
<main data-measure="center">

0 commit comments

Comments
 (0)