question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redefine useAsync to separate config async config from parameters to the promiseFn

See original GitHub issue

This 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
ghengeveldcommented, Feb 26, 2020

There’s this PR by the way: https://github.com/async-library/react-async/pull/247

Would be great if someone could review it.

1reaction
ghengeveldcommented, Feb 26, 2020

I’m still down for this change. Just swamped at the moment.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found