Skip to content

Commit 68759e8

Browse files
committed
Improve types for promiseFn/deferFn props.
1 parent 5e8e426 commit 68759e8

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

examples/with-typescript/src/App.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
import React, { Component } from "react"
2-
import Async, { createInstance, useAsync, IfPending, IfRejected, IfFulfilled } from "react-async"
2+
import Async, {
3+
createInstance,
4+
useAsync,
5+
IfPending,
6+
IfRejected,
7+
IfFulfilled,
8+
PromiseFn,
9+
} from "react-async"
310
import DevTools from "react-async-devtools"
411
import "./App.css"
512

6-
const promiseFn = () => Promise.resolve("baz")
7-
const CustomAsync = createInstance({ promiseFn })
13+
const loadFirstName: PromiseFn<string> = ({ userId }) =>
14+
fetch(`https://reqres.in/api/users/${userId}`)
15+
.then(res => (res.ok ? Promise.resolve(res) : Promise.reject(res)))
16+
.then(res => res.json())
17+
.then(({ data }) => data.first_name)
18+
19+
const CustomAsync = createInstance({ promiseFn: loadFirstName })
820

921
const UseAsync = () => {
10-
const state = useAsync({ promiseFn })
22+
const state = useAsync({ promiseFn: loadFirstName, userId: 1 })
1123
return (
1224
<>
1325
<IfPending state={state}>Loading...</IfPending>
@@ -34,7 +46,7 @@ class App extends Component {
3446
<Async promiseFn={() => Promise.resolve("bar")}>
3547
<Async.Resolved>{data => <>{data}</>}</Async.Resolved>
3648
</Async>
37-
<CustomAsync>
49+
<CustomAsync userId={1}>
3850
<CustomAsync.Resolved>{data => <>{data}</>}</CustomAsync.Resolved>
3951
</CustomAsync>
4052
<UseAsync />

packages/react-async/src/index.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ export type SettledChildren<T> =
1313
| ((state: AsyncFulfilled<T> | AsyncRejected<T>) => React.ReactNode)
1414
| React.ReactNode
1515

16-
export type PromiseFn<T> = (props: object, controller: AbortController) => Promise<T>
17-
export type DeferFn<T> = (args: any[], props: object, controller: AbortController) => Promise<T>
16+
export type PromiseFn<T> = (props: AsyncProps<T>, controller: AbortController) => Promise<T>
17+
export type DeferFn<T> = (
18+
args: any[],
19+
props: AsyncProps<T>,
20+
controller: AbortController
21+
) => Promise<T>
1822

1923
interface AbstractAction {
2024
type: string
@@ -31,7 +35,7 @@ export interface AsyncOptions<T> {
3135
promiseFn?: PromiseFn<T>
3236
deferFn?: DeferFn<T>
3337
watch?: any
34-
watchFn?: (props: object, prevProps: object) => any
38+
watchFn?: (props: AsyncProps<T>, prevProps: AsyncProps<T>) => any
3539
initialValue?: T
3640
onResolve?: (data: T) => void
3741
onReject?: (error: Error) => void
@@ -43,7 +47,7 @@ export interface AsyncOptions<T> {
4347
dispatcher?: (
4448
action: AsyncAction<T>,
4549
internalDispatch: (action: AsyncAction<T>) => void,
46-
props: object
50+
props: AsyncProps<T>
4751
) => void
4852
debugLabel?: string
4953
[prop: string]: any

0 commit comments

Comments
 (0)