Redefine useAsync to separate config async config from parameters to the promiseFn
See original GitHub issueThis is a proposal for a change in the way useAsync works. I propose to add a third parameter, which is an object which is passed as the first argument to the promiseFn.
Currently this information is passed along in the AsyncOptions as [prop: string]: any:
export interface AsyncOptions<T> {
promise?: Promise<T>
promiseFn?: PromiseFn<T>
// abbreviated
debugLabel?: string
[prop: string]: any
}
This makes the arguments to the promiseFn unsafe. Which is not nice for the developer experience. Plus it makes the AsyncOptions have a strange dual role.
My Proposal would create a dedicated parameter for the parameters to the promiseFn function like so :
type LoadDataConfig = {
id: string
}
export function loadData(config: LoadDataConfig, props: AsyncProps<Link>, controller: AbortController) {
return Link.one(config.id);
}
export default function AwesomeComponent() {
const { id } = useParams();
const state = useAsync(loadLink, { id }, { watch: id });
}
This would also mean redefining PromiseFn like so:
export type PromiseFn<T, C> = (config: C, props: AsyncProps<T>, controller: AbortController) => Promise<T>
Another less breaking options would be to add the config to AsyncOptions:
export interface AsyncOptions<T, C> {
promise?: Promise<T>
promiseFn?: PromiseFn<T>
// abbreviated
debugLabel?: string
config: C
[prop: string]: any
}
This would at least make the config type safe, but makes the api a little nicer.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Configuration options - React Async
Configuration options. These can be passed in an object to useAsync(options) , or as props to <Async {...options}> and custom instances.
Read more >Typing issue with useAsync and TypeScript #256 - GitHub
Experiencing TypeScript errors when using useAsync version 10.0.0. ... Redefine useAsync to separate config async config from parameters to ...
Read more >react-async - npm
React Async offers three primary APIs: the useAsync hook, the <Async> ... { headers }, options) // This will setup a promiseFn with...
Read more >useAsync React Hook - useHooks
Possible values for status prop are: "idle", "pending", "success", "error". As you'll see in the code below, our hook allows both immediate ...
Read more >The Power and Convenience of useAsync | by Jennifer Fu
useAsync Hook · promiseFn : Function that returns a Promise, automatically invoked. · onResolve : Callback invoked when the Promise resolves.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

There’s this PR by the way: https://github.com/async-library/react-async/pull/247
Would be great if someone could review it.
I’m still down for this change. Just swamped at the moment.