{"id":16810906,"url":"https://github.com/isthatcentered/use-promise-machine","last_synced_at":"2025-03-17T10:41:24.805Z","repository":{"id":56535459,"uuid":"185395064","full_name":"isthatcentered/use-promise-machine","owner":"isthatcentered","description":"React promises as a state machine. Tells you explicitly if your promise is PENDING || FULFILLED_DATA || FULFILLED_EMPTY || REJECTED","archived":false,"fork":false,"pushed_at":"2020-11-02T04:38:06.000Z","size":551,"stargazers_count":0,"open_issues_count":14,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-25T19:02:28.637Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/isthatcentered.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-05-07T12:14:28.000Z","updated_at":"2019-05-07T13:43:17.000Z","dependencies_parsed_at":"2022-08-15T20:31:16.067Z","dependency_job_id":null,"html_url":"https://github.com/isthatcentered/use-promise-machine","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isthatcentered%2Fuse-promise-machine","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isthatcentered%2Fuse-promise-machine/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isthatcentered%2Fuse-promise-machine/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/isthatcentered%2Fuse-promise-machine/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/isthatcentered","download_url":"https://codeload.github.com/isthatcentered/use-promise-machine/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244018866,"owners_count":20384649,"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-10-13T10:17:03.887Z","updated_at":"2025-03-17T10:41:24.764Z","avatar_url":"https://github.com/isthatcentered.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# usePromiseMachine\nBecause having to set `loading` and `error` and doing some `if (data)`  for promises is boring 🙃. \n\n## Features\n- Know instantly which state your promise is in via `myPromise.state` *(-\u003e `PENDING` || `FULFILLED_DATA` || `FULFILLED_EMPTY` || `REJECTED`)*\n- Easily access the error or the data via `myPromise.error` or `myPromise.data`\n- Don't bother guessing if you're getting back an empty container (`[]` or `{}`). If it's the case, `myPromise.state` will be `FULFILLED_EMPTY`\n- No need to remember the existing states, they're all available via `usePromiseMachine.STATES.*`\n- Simpler code, safe, 0 brain power required 🥳\n\n## Install\n```bash\nnpm i use-promise-machine -D\n```\n\n## Use\n```typescript jsx\nimport { usePromiseMachine } from \"use-promise-machine\"\n\n\nfunction _HomePage( props: {})\n{\n  // (use useCallback if you're using an inline function to avoir infinite rendering) \n  const myFunctionThatReturnsAPromise = useCallback(() =\u003e fetch(\"/waffles\")),\n        dataPromise                    = usePromiseMachine(myFunctionThatReturnsAPromise)\n  \n  return (\n    \u003cdiv\u003e\n      {(() =\u003e {\n        switch ( dataPromise.state ) {\n          case usePromiseMachine.STATES.PENDING:\n            return \u003cp\u003eLoading...\u003c/p\u003e\n          \n          case usePromiseMachine.STATES.REJECTED:\n            return \u003cp\u003e💩 Something went wrong\u003c/p\u003e\n  \n          // You can merge switch cases if you don't care about data or empty state        \n          case usePromiseMachine.STATES.FULFILLED_DATA:\n          case usePromiseMachine.STATES.FULFILLED_EMPTY:\n            return \u003cSomeComponent users={dataPromise.data} /\u003e\n            \n          // will never be reached\n          default:\n          \treturn null\n        }\n      })()}\n    \u003c/div\u003e)\n}\n```\n\n## Fun facts\n- You can access every state the promise can be in by using the `usePromise.STATES` variable.\n- The `usePromise.STATES.FULFILLED_EMPTY` means your promise returns either an empty `array` or an empty `object`\n\n## Learn more\nCheck the tests folder in `src/usePromise.spec.ts` or [tweet me 😉](https://twitter.com/isthatcentered)  ","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisthatcentered%2Fuse-promise-machine","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fisthatcentered%2Fuse-promise-machine","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fisthatcentered%2Fuse-promise-machine/lists"}