{"id":18647626,"url":"https://github.com/artcom/async-task-hook","last_synced_at":"2025-11-05T06:30:35.320Z","repository":{"id":42431605,"uuid":"271831319","full_name":"artcom/async-task-hook","owner":"artcom","description":"Provides an react hook for async tasks.","archived":false,"fork":false,"pushed_at":"2023-03-14T23:27:39.000Z","size":1411,"stargazers_count":0,"open_issues_count":5,"forks_count":0,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-01-29T04:37:41.028Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/artcom.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-12T15:29:40.000Z","updated_at":"2022-04-06T11:13:51.000Z","dependencies_parsed_at":"2024-11-07T06:28:46.678Z","dependency_job_id":"fa3196d3-875c-4f9e-9051-9cb701b7a2c4","html_url":"https://github.com/artcom/async-task-hook","commit_stats":{"total_commits":22,"total_committers":2,"mean_commits":11.0,"dds":"0.045454545454545414","last_synced_commit":"10fbe7fea92684205dc983d67853b599f44a3f26"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artcom%2Fasync-task-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artcom%2Fasync-task-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artcom%2Fasync-task-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/artcom%2Fasync-task-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/artcom","download_url":"https://codeload.github.com/artcom/async-task-hook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239449583,"owners_count":19640535,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-11-07T06:27:11.902Z","updated_at":"2025-11-05T06:30:35.273Z","avatar_url":"https://github.com/artcom.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Async Task Hook\n\nProvides an react hook for async tasks.\n\n## Features\n\nRuns a given async task in a [useEffect](https://reactjs.org/docs/hooks-reference.html#useeffect) hook. The returned state contains the status of the runnning task as well as the result or error. The task completion will be ignored if the component has been unmounted.\n\nNote: The given task function must not change (e.g. wrapped with [useCallback](https://reactjs.org/docs/hooks-reference.html#usecallback)) otherwise a new task (read [useEffect](https://reactjs.org/docs/hooks-reference.html#useeffect)) will be started and the previous result will be ignored.\n\n## States\n\n* `RUNNING` the initial state and the state while the task is running.\n* `FINISHED` the final state as soon as the method has finished.\n* `ERROR` the final state if the task threw an error.\n\n## Example\n\nThis example queries content with [axios](https://github.com/axios/axios). The component receives the `url` as property and creates a persistent `queryFunc` which is fed to the async task hook. The returned result can be accessed when the async task has `FINISHED`. If the method throws an exception the status will be set to `ERROR`\n\n```javascript\nimport axios from 'axios'\nimport React, { useCallback } from \"react\"\nimport { useAsyncTask, RUNNING, FINISHED, ERROR } from \"@artcom/async-task-hook\"\n\nconst MyComponent = ({ url }) =\u003e {\n  const queryFunc = useCallback(() =\u003e axios.get(url), [url])\n  const query = useAsyncTask(queryFunc)\n\n  switch(query.status) {\n    case RUNNING: return \u003c\u003eLoading...\u003c/\u003e\n    case FINISHED: return \u003c\u003equery.result\u003c/\u003e\n    case ERROR: return \u003c\u003equery.error\u003c/\u003e\n  }\n}\n```\n\n## Tests\n\nCheckout the [tests](./test/useAsyncTask.test.js) for different scenarios.\n\n```bash\nnpm i \u0026\u0026 npm run test\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartcom%2Fasync-task-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fartcom%2Fasync-task-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fartcom%2Fasync-task-hook/lists"}