{"id":40608543,"url":"https://github.com/azmenak/use-async-call","last_synced_at":"2026-01-21T05:06:37.289Z","repository":{"id":34306295,"uuid":"176862216","full_name":"azmenak/use-async-call","owner":"azmenak","description":"Provides a hook to manage the state and lifecycle of async data loading","archived":false,"fork":false,"pushed_at":"2023-01-03T18:14:32.000Z","size":934,"stargazers_count":11,"open_issues_count":12,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-09T18:30:18.272Z","etag":null,"topics":["async","react","react-hooks","typescript","usereducer"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/azmenak.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-21T03:23:23.000Z","updated_at":"2019-09-19T06:34:45.000Z","dependencies_parsed_at":"2023-01-15T06:11:47.702Z","dependency_job_id":null,"html_url":"https://github.com/azmenak/use-async-call","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/azmenak/use-async-call","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azmenak%2Fuse-async-call","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azmenak%2Fuse-async-call/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azmenak%2Fuse-async-call/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azmenak%2Fuse-async-call/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/azmenak","download_url":"https://codeload.github.com/azmenak/use-async-call/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/azmenak%2Fuse-async-call/sbom","scorecard":{"id":221251,"data":{"date":"2025-08-11","repo":{"name":"github.com/azmenak/use-async-call","commit":"ee9659bc945d5da423379c5b69bca499ea0a27d3"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":1.3,"checks":[{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"SAST","score":0,"reason":"no SAST tool detected","details":["Warn: no pull requests merged into dev branch"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Code-Review","score":0,"reason":"Found 0/30 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":0,"reason":"license file not detected","details":["Warn: project does not have a license file"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"Vulnerabilities","score":0,"reason":"53 existing vulnerabilities detected","details":["Warn: Project is vulnerable to: GHSA-968p-4wvh-cqc8","Warn: Project is vulnerable to: GHSA-67hx-6x53-jw92","Warn: Project is vulnerable to: GHSA-6chw-6frg-f759","Warn: Project is vulnerable to: GHSA-v88g-cgmw-v5xw","Warn: Project is vulnerable to: GHSA-93q8-gq69-wqmw","Warn: Project is vulnerable to: GHSA-v6h2-p8h4-qcjw","Warn: Project is vulnerable to: GHSA-grv7-fg5c-xmjg","Warn: Project is vulnerable to: GHSA-w8qv-6jwh-64r5","Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275","Warn: Project is vulnerable to: GHSA-gxpj-cx7g-858c","Warn: Project is vulnerable to: GHSA-w573-4hg7-7wgq","Warn: Project is vulnerable to: GHSA-fjxv-7rqg-78g4","Warn: Project is vulnerable to: GHSA-8r6j-v8pm-fqw3","Warn: Project is vulnerable to: MAL-2023-462","Warn: Project is vulnerable to: GHSA-w457-6q6x-cgp9","Warn: Project is vulnerable to: GHSA-62gr-4qp9-h98f","Warn: Project is vulnerable to: GHSA-f52g-6jhx-586p","Warn: Project is vulnerable to: GHSA-2cf5-4w76-r9qv","Warn: Project is vulnerable to: GHSA-3cqr-58rm-57f8","Warn: Project is vulnerable to: GHSA-g9r4-xpmj-mj65","Warn: Project is vulnerable to: GHSA-q2c6-c6pm-g3gh","Warn: Project is vulnerable to: GHSA-765h-qjxv-5f44","Warn: Project is vulnerable to: GHSA-f2jv-r9rf-7988","Warn: Project is vulnerable to: GHSA-43f8-2h32-f4cj","Warn: Project is vulnerable to: GHSA-qqgx-2p2h-9c37","Warn: Project is vulnerable to: GHSA-896r-f27r-55mw","Warn: Project is vulnerable to: GHSA-9c47-m6qq-7p4h","Warn: Project is vulnerable to: GHSA-6c8f-qphg-qjgp","Warn: Project is vulnerable to: GHSA-p6mc-m468-83gw","Warn: Project is vulnerable to: GHSA-29mw-wpgm-hmr9","Warn: Project is vulnerable to: GHSA-35jh-r3h4-6jhm","Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv","Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3","Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m","Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h","Warn: Project is vulnerable to: GHSA-5fw9-fq32-wv5p","Warn: Project is vulnerable to: GHSA-hj48-42vr-x3v9","Warn: Project is vulnerable to: GHSA-hrpp-h998-j3pp","Warn: Project is vulnerable to: GHSA-p8p7-x288-28g6","Warn: Project is vulnerable to: GHSA-gcx4-mw62-g8wm","Warn: Project is vulnerable to: GHSA-c2qf-rxjj-qqgw","Warn: Project is vulnerable to: GHSA-3jfq-g458-7qm9","Warn: Project is vulnerable to: GHSA-r628-mhmh-qjhw","Warn: Project is vulnerable to: GHSA-9r2w-394v-53qc","Warn: Project is vulnerable to: GHSA-5955-9wpr-37jh","Warn: Project is vulnerable to: GHSA-qq89-hq3f-393p","Warn: Project is vulnerable to: GHSA-f5x3-32g6-xq36","Warn: Project is vulnerable to: GHSA-jgrx-mgxx-jf9v","Warn: Project is vulnerable to: GHSA-72xf-g2v4-qvf3","Warn: Project is vulnerable to: GHSA-6fc8-4gx4-v693","Warn: Project is vulnerable to: GHSA-3h5v-q93c-6h6q","Warn: Project is vulnerable to: GHSA-c4w7-xm78-47vh","Warn: Project is vulnerable to: GHSA-p9pc-299p-vxgp"],"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}}]},"last_synced_at":"2025-08-17T02:33:44.453Z","repository_id":34306295,"created_at":"2025-08-17T02:33:44.454Z","updated_at":"2025-08-17T02:33:44.454Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28627388,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-21T04:47:28.174Z","status":"ssl_error","status_checked_at":"2026-01-21T04:47:22.943Z","response_time":86,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","react","react-hooks","typescript","usereducer"],"created_at":"2026-01-21T05:06:37.234Z","updated_at":"2026-01-21T05:06:37.282Z","avatar_url":"https://github.com/azmenak.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# use-async-call\n\n![npm](https://img.shields.io/npm/v/use-async-call.svg)\n[![codecov](https://codecov.io/gh/azmenak/use-async-call/branch/master/graph/badge.svg)](https://codecov.io/gh/azmenak/use-async-call)\n[![Build Status](https://travis-ci.org/azmenak/use-async-call.svg?branch=master)](https://travis-ci.org/azmenak/use-async-call)\n![NPM](https://img.shields.io/npm/l/use-async-call.svg)\n\nProvides an abstraction over the lower-level [`use-async-reducer`](https://github.com/azmenak/use-async-reducer), handles calls to `useEffect` and handles cancelation when the inputs change or components unmounts to avoid modifying stale data\n\n## Install\n\n```\nnpm install use-async-call\n```\n\n## Usage\n\n```ts\nimport useAsyncCall from 'use-async-call'\n\nconst [state, {update, refresh, actions}] = useAsyncCall(\n  asyncCreator,\n  (options = {})\n)\n```\n\n### Demo\n\n[![Edit use-async-call Basic Demo](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/mo51y75rzx?fontsize=14)\n\n### Params\n\n- `asyncCreator` An async method (returns a promise), create this method with `useCallback` if it depends on state from the component\n\n```ts\nconst [searchText, setSearchText] = useState('')\nconst fetchData = useCallback(() =\u003e Api.search(searchText), [searchText])\n```\n\n- `options`\n\n```ts\ninterface UseAsyncCallOptions\u003cT\u003e {\n  /**\n   * Initial value used for `data` of state\n   */\n  initialValue?: T\n\n  /**\n   * When true, will not call `actions.initalize` when `asyncCreator` updates\n   * This keeps the data in the store between updates, useful when the identity\n   * of the data does not belong to the inputs, example would be a search\n   * component that uses \"search text\" as an input\n   */\n  dontReinitialize?: boolean\n\n  /**\n   * Callback called after call is successful\n   * @param data Data returned from async caller\n   */\n  onSuccess?(data?: T): void\n\n  /**\n   * Callback called after async call throws\n   * @param error Error thrown by async caller\n   */\n  onFailure?(error?: Error): void\n\n  /**\n   * Callback always called after async call completes\n   */\n  onComplete?(): void\n}\n```\n\n### Return values\n\n- `state` an object containing state of async call\n\n```ts\nconst state: Loadable = {\n  data: {}, // any data\n  loading: false, // true when calls in progress\n  error: null // instance of Error if calls throw\n}\n```\n\n- `update(asyncUpdater, updateOptions = {})` method used to update the state\n\n  - `asyncUpdater` either a promise or a method which returns a promise, the result will be set to the `data` value of the state\n  - `updateOptions`\n\n  ```ts\n  interface UseAsyncCallUpdateOptions\u003cT\u003e {\n    /**\n     * Should thrown errors be re-thrown in the resulting promise from `update`;\n     * useful when using in conjuction with form libraries that expect errors\n     * when submitting form values\n     */\n    throwError?: boolean\n\n    /**\n     * If the caller throws, sets `state.error` to the error and `state.data` to\n     * `null`\n     */\n    saveError?: boolean\n\n    /**\n     * Callback called after call is successful\n     * @param data Data returned from async caller\n     */\n    onSuccess?(data?: T): void\n\n    /**\n     * Callback called after async call throws\n     * @param error Error thrown by async caller\n     */\n    onFailure?(error?: Error): void\n\n    /**\n     * Callback always called after async call completes\n     */\n    onComplete?(): void\n  }\n  ```\n\n- `refresh` method used to re-call the method passed to `useAsyncCall`\n\n- `actions` action methods created by [`use-async-reducer`](https://github.com/azmenak/use-async-reducer)\n\n```ts\ninterface AsyncReducerBoundActions\u003cT = any\u003e {\n  /**\n   * To be called at the beginning of a request, sets `loading` to `true`\n   */\n  request(): void\n  /**\n   * To be called with the data to be saved into the state\n   * @param payload Result of the async call\n   */\n  success(payload: T): void\n  /**\n   * To be called when the async call fails\n   * @param error\n   */\n  failure(error: Error): void\n  /**\n   * Can be called when a call fails/complete and the result is being discarded\n   */\n  complete(): void\n}\n```\n\n## Examples\n\n### Basic Example\n\n```tsx\nimport React, {useCallback} from 'react'\nimport useAsyncCall from 'use-async-call'\n\nimport Api from './custom-api'\n\nconst DataLoadingComponent: React.FC\u003c{id: number}\u003e = ({id}) =\u003e {\n  const fetchData = useCallback(() =\u003e Api.fetchModelData(id), [id])\n\n  const [model] = useAsyncCall(fetchData)\n\n  // model is now managed, it will automatically fetch new data when `id` prop\n  // changes and update the state to reflect any changes\n}\n```\n\n### A component which updates a value at an API\n\n```tsx\nimport React, {useCallback} from 'react'\nimport useAsyncCall from 'use-async-call'\n\nimport Api from './custom-api'\n\ninterface User {\n  id: number\n  name: string\n}\n\nconst UserProfile: React.FC\u003c{userId: number}\u003e = ({userId}) =\u003e {\n  const [name, setName] = useState('')\n\n  const fetchUser = useCallback(() =\u003e Api.fetchUserById(userId), [userId])\n  const [user, {update: updateUser}] = useAsyncCall\u003cUser\u003e(fetchUser)\n\n  const handleUpdateUserName = useCallback((): Promise\u003cUser\u003e =\u003e {\n    return Api.updateUser(userId, {name})\n  }, [userId, name])\n\n  return (\n    \u003cdiv\u003e\n      \u003ch1\u003eUser: {userId}\u003c/h1\u003e\n      {user.loading \u0026\u0026 \u003cdiv\u003eLoading...\u003c/div\u003e}\n      {user.data \u0026\u0026 \u003cdiv\u003e{user.data.name}\u003c/div\u003e}\n      {user.error \u0026\u0026 \u003cdiv\u003e{user.error.message}\u003c/div\u003e}\n\n      {user.data \u0026\u0026 (\n        \u003c\u003e\n          \u003cinput\n            value={name}\n            onChange={(event) =\u003e setName(event.target.value)}\n          /\u003e\n          \u003cbutton\n            disabled={user.loading}\n            onClick={() =\u003e updateUser(handleUpdateUserName)}\n          \u003e\n            Update Name\n          \u003c/button\u003e\n        \u003c/\u003e\n      )}\n    \u003c/div\u003e\n  )\n}\n```\n\n### Create a custom hook to load and update a model\n\n```ts\nimport {useCallback} from 'react'\nimport useAsyncCall, {Loadable} from 'use-async-call'\n\nimport Api from './custom-api'\n\ninterface User {\n  id: number\n  name: string\n}\n\nexport function useUserData(\n  userId: number\n): [Loadable\u003cUser\u003e, (userData: Partial\u003cUser\u003e) =\u003e Promise\u003cUser\u003e] {\n  const fetchUser = useCallback(() =\u003e Api.fetchUserById(userId), [userId])\n  const [user, {update: updateUser}] = useAsyncCall\u003cUser\u003e(fetchUser)\n\n }, [])\n  const handleUpdateUser = useCallback(\n    (userData: Partial\u003cUser\u003e) =\u003e {\n      return updateUser(Api.updateUser(userId, userData), {\n        onSuccess() {\n          alert('Updated user!')\n        },\n        onFailure() {\n          alert('Failed to update user')\n        }\n      })\n    },\n    [userId]\n  )\n\n  return [user, handleUpdateUser]\n}\n```\n\n### Get Data from a Search API\n\n```tsx\nimport React, {useCallback, useState} from 'react'\nimport useAsyncCall from 'use-async-call'\n\nimport SearchApi from './search-api'\n\nexport function useSearchData(searchText: string) {\n  const fetchData = useCallback(() =\u003e SearchApi.find(searchText), [searchText])\n\n  return useAsyncCall(fetchData, {dontReinitialize: true})\n}\n\nconst SearchComponent: React.FC = () =\u003e {\n  const [searchText, setSearchText] = useState('')\n  const [searchData] = useSearchData(searchText)\n\n  return (\n    \u003c\u003e\n      \u003cinput\n        value={searchText}\n        onChange={(event) =\u003e {\n          setSearchText(event.target.value)\n        }}\n      /\u003e\n      {searchData.data \u0026\u0026 (\n        \u003c\u003e\n          \u003ch1\u003eSearch Results\u003c/h1\u003e\n          \u003cul\u003e\n            {searchData.data.map((searchResult) =\u003e (\n              \u003cli key={searchResult.id}\u003e{searchResult.name}\u003c/li\u003e\n            ))}\n          \u003c/ul\u003e\n        \u003c/\u003e\n      )}\n    \u003c/\u003e\n  )\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazmenak%2Fuse-async-call","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fazmenak%2Fuse-async-call","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fazmenak%2Fuse-async-call/lists"}