{"id":23366708,"url":"https://github.com/d8corp/async-mobx","last_synced_at":"2025-04-07T23:15:40.921Z","repository":{"id":42798379,"uuid":"271982874","full_name":"d8corp/async-mobx","owner":"d8corp","description":"Get async data for MobX","archived":false,"fork":false,"pushed_at":"2023-01-06T08:47:11.000Z","size":931,"stargazers_count":2,"open_issues_count":12,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T13:04:56.384Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/d8corp.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-13T09:58:28.000Z","updated_at":"2021-01-14T19:10:59.000Z","dependencies_parsed_at":"2023-02-05T18:01:55.438Z","dependency_job_id":null,"html_url":"https://github.com/d8corp/async-mobx","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d8corp%2Fasync-mobx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d8corp%2Fasync-mobx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d8corp%2Fasync-mobx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/d8corp%2Fasync-mobx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/d8corp","download_url":"https://codeload.github.com/d8corp/async-mobx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247744328,"owners_count":20988783,"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-12-21T14:17:21.802Z","updated_at":"2025-04-07T23:15:40.903Z","avatar_url":"https://github.com/d8corp.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# async-mobx\n[![NPM](https://img.shields.io/npm/v/async-mobx.svg)](https://github.com/d8corp/async-mobx/blob/master/CHANGELOG.md)\n[![downloads](https://img.shields.io/npm/dm/async-mobx.svg)](https://www.npmjs.com/package/async-mobx)\n[![license](https://img.shields.io/npm/l/async-mobx)](https://github.com/d8corp/async-mobx/blob/master/LICENSE)  \nGetting async data for [Mobx 3+](https://mobx.js.org/README.html).\n### Installation\nnpm\n```bash\nnpm i async-mobx\n```\nyarn\n```bash\nyarn add async-mobx\n```\n### Using\n`Async` is a `Promise` like constructor\n```javascript\nimport Async from 'async-mobx'\n\nconst promise = new Async((resolve, reject) =\u003e {\n  fetch('/test').then(resolve, reject)\n})\n```\n### then, catch, finally\n`then`, `catch` and `finally` always return instance of `Promise`\n```javascript\nconst test = new Async().then() instanceof Promise\n// test === true \n```\nUse `then`, `catch` and `finally` like for `Promise`\n```javascript\nconst promise = new Async(resolve =\u003e resolve(1))\npromise\n  .then(value =\u003e console.log('then', value))\n  .finally(value =\u003e console.log('finally', value))\n  .catch(value =\u003e console.log('catch', value))\n```\nWe have one specific think for mobx, if you return a function to `resolve` or `reject` then the function will be called when you need to get the result\n```javascript\n(async () =\u003e {\n  let test = true\n  const promise = new Async(resolve =\u003e resolve(() =\u003e test = false))\n  // test still equals true\n  await promise\n  // test is false\n})()\n```\nYou may override the result at function to fix it\n```javascript\n(async () =\u003e {\n  function test () {}\n  const promise = new Async(resolve =\u003e resolve(() =\u003e test))\n  const result = await promise\n  return result === test // true\n})()\n```\n### loading\nYou may check status of `Async` with `loading`, it's `true` when data is loading\n```javascript\n(async () =\u003e {\n  const promise = new Async(resolve =\u003e setTimeout(resolve))\n  // promise.loading === true\n  await promise\n  // promise.loading === false\n})()\n```\n### loaded\nYou may check status of `Async` with `loaded`, it's `true` when data was loaded at least one time\n```javascript\n(async () =\u003e {\n  const promise = new Async(resolve =\u003e setTimeout(resolve))\n  // promise.loaded === false\n  await promise\n  // promise.loaded === true\n})()\n```\n### value\nYou may get result without `await` synchronously with `value`\n```javascript\nconst promise = new Async(resolve =\u003e resolve(1))\n// promise.value === 1\n```\nBut `value` returns result at the moment\n```javascript\n(async () =\u003e {\n  const promise = new Async(resolve =\u003e setTimeout(() =\u003e resolve(1)))\n  // promise.value === undefined\n  await promise\n  // promise.value === 1\n})()\n```\n### error\nYou may handle error without `await` synchronously with `error` like `value` with `resolve`\n```javascript\nconst promise = new Async((resolve, reject) =\u003e reject(1))\n// promise.error === 1\n```\n### default\nYou may provide default `value` for `Async`\n```javascript\n(async () =\u003e {\n  const promise = new Async({\n    request: resolve =\u003e setTimeout(() =\u003e resolve(2)),\n    default: 1\n  })\n  // promise.value === 1\n  await promise\n  // promise.value === 2\n})()\n```\n### response\n`response` is the same `value` but without default value\n```javascript\n(async () =\u003e {\n  const promise = new Async({\n    request: resolve =\u003e setTimeout(() =\u003e resolve(2)),\n    default: 1\n  })\n  // promise.value === 1\n  // promise.response === undefined\n  await promise\n  // promise.value === 2\n  // promise.response === 2\n})()\n```\n### update\nUnlike `Promise`, you may reuse `Async` with `update` method\n```javascript\nlet i = 0\nconst promise = new Async(resolve =\u003e resolve(i++))\n// i === 1\npromise.update()\n// i === 2\n```\n### reset\nThe method just sets default value as current and clears `error`\n```javascript\nconst promise = new Async({default: 1})\npromise.resolve(2) // promise.value = 2\npromise.reset() // promise.value = 1\n```\n### resolve\nYou may use `resolve` to say async that loading is finished successfully\n```javascript\nconst promise = new Async()\npromise.resolve(1)\n// promise.value === 1\n```\n### reject\nYou may use `reject` to say async that loading is finished with error\n```javascript\nconst promise = new Async()\npromise.reject(1)\n// promise.error === 1\n```\n### on, once, off\nYou may add a listener to react on events.  \nUse `resolve`, `reject` or `update` as a type of event.\n```javascript\nconst promise = new Async()\nlet test = false\npromise.on('resolve', () =\u003e test = promise.value)\n// test === false\npromise.resolve(true)\n// test === true\npromise.resolve(false)\n// test === false\n```\nYou may add a listener which reacts only once with `once`\n```javascript\nconst promise = new Async()\nlet test = false\npromise.once('resolve', () =\u003e test = promise.value)\n// test === false\npromise.resolve(true)\n// test === true\npromise.resolve(false)\n// test === true\n```\nYou may turn off a listener\n```javascript\nconst promise = new Async()\nlet test = false\nconst listener = () =\u003e test = promise.value\npromise.on('resolve', listener)\n// test === false\npromise.resolve(true)\n// test === true\npromise.off('resolve', listener)\npromise.resolve(false)\n// test === true\n```\n## Issues\nIf you find a bug, please file an issue on [GitHub](https://github.com/d8corp/async-mobx/issues)  \n[![issues](https://img.shields.io/github/issues-raw/d8corp/async-mobx)](https://github.com/d8corp/async-mobx/issues)  \n\u003e ---\n[![stars](https://img.shields.io/github/stars/d8corp/async-mobx?style=social)](https://github.com/d8corp/async-mobx/stargazers)\n[![watchers](https://img.shields.io/github/watchers/d8corp/async-mobx?style=social)](https://github.com/d8corp/async-mobx/watchers)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd8corp%2Fasync-mobx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fd8corp%2Fasync-mobx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fd8corp%2Fasync-mobx/lists"}