{"id":21697507,"url":"https://github.com/silvestrevivo/react-mobx-async","last_synced_at":"2026-04-16T10:32:02.398Z","repository":{"id":97115786,"uuid":"141145738","full_name":"silvestrevivo/react-mobx-async","owner":"silvestrevivo","description":"Explanation of different approaches in MobX about async/await for fetching data","archived":false,"fork":false,"pushed_at":"2018-07-16T21:33:55.000Z","size":17,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-24T03:52:38.023Z","etag":null,"topics":["async-await","fetch-api","mobx","observable","parceljs","reactjs","webapp"],"latest_commit_sha":null,"homepage":null,"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/silvestrevivo.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":"2018-07-16T13:55:12.000Z","updated_at":"2018-07-16T21:55:01.000Z","dependencies_parsed_at":"2023-04-27T22:49:44.109Z","dependency_job_id":null,"html_url":"https://github.com/silvestrevivo/react-mobx-async","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/silvestrevivo/react-mobx-async","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silvestrevivo%2Freact-mobx-async","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silvestrevivo%2Freact-mobx-async/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silvestrevivo%2Freact-mobx-async/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silvestrevivo%2Freact-mobx-async/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/silvestrevivo","download_url":"https://codeload.github.com/silvestrevivo/react-mobx-async/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/silvestrevivo%2Freact-mobx-async/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31882046,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T09:23:21.276Z","status":"ssl_error","status_checked_at":"2026-04-16T09:23:15.028Z","response_time":69,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6: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-await","fetch-api","mobx","observable","parceljs","reactjs","webapp"],"created_at":"2024-11-25T19:28:03.354Z","updated_at":"2026-04-16T10:32:02.381Z","avatar_url":"https://github.com/silvestrevivo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n  \u003cimg width=\"200\" height=\"200\"\n    src=\"https://sandstorm.de/_Resources/Persistent/3285416e8503b2c8354c321bcd690cf550b8b2d3/React-Logo.svg\"\u003e\n  \u003ca href=\"https://github.com/mobxjs/mobx\"\u003e\n    \u003cimg width=\"200\" height=\"200\"\n      src=\"https://mobx.js.org/docs/mobx.png\"\u003e\n  \u003c/a\u003e\n  \u003ch1\u003eReact MobX async\u003c/h1\u003e\n\u003c/div\u003e\n\nExplanation of different approaches in MobX about async/await for fetching data. A first very simple version would be something like this:\n\n```javascript\nimport { action, observable } from 'mobx'\n\nclass WeatherStore {\n  @observable weatherData = {};\n\n  @action loaderWeather = (city) =\u003e {\n    fetch(`https://abnormal-weather-api.herokuapp.com/cities/search?city=${city}`)\n      .then(response =\u003e response.json())\n      .then(data =\u003e {\n        this.weatherData = data\n        console.log('data', data)\n      })\n  }\n}\n\nvar store = new WeatherStore()\n\nexport default store\n```\nIn MobX4, when we use __enforceActions: true__ we have to take out the change of\nthe _observable_ from the promise's callback:\n\n```javascript\nclass WeatherStore {\n  @observable weatherData = {};\n\n  @action loaderWeather = (city) =\u003e {\n    fetch(`https://abnormal-weather-api.herokuapp.com/cities/search?city=${city}`)\n      .then(response =\u003e response.json())\n      .then(data =\u003e {\n        this.setWeather(data)\n        console.log('data', data)\n      })\n  }\n\n  setWeather = data =\u003e {\n    this.weatherData = data\n  }\n}\n```\n\nOther solution will be using runInAction() inside of the promises:\n\n```javascript\nclass WeatherStore {\n  @observable weatherData = {};\n\n  @action loaderWeather = (city) =\u003e {\n    fetch(`https://abnormal-weather-api.herokuapp.com/cities/search?city=${city}`)\n      .then(response =\u003e response.json())\n      .then(data =\u003e {\n        runInAction(() =\u003e {\n          this.weatherData = data\n          console.log('data', data)\n        })\n      })\n  }\n}\n```\nBut the best option is running __generators__ to control de _async/await_ flow\nof the data fetching:\n\n```javascript\nimport {\n  configure,\n  observable,\n  flow } from 'mobx'\n\nconfigure({enforceActions: true})\n\nclass WeatherStore {\n  @observable weatherData = {};\n\n  loadWeatherGenerator = flow(function * (city) {\n    const response = yield fetch(\n      `https://abnormal-weather-api.herokuapp.com/cities/search?city=${city}`\n    )\n    const data = yield response.json()\n    this.weatherData = data\n    console.log('data', data)\n  });\n}\n\nvar store = new WeatherStore()\n\nexport default store\n```\n\nHere inside is possile to use __try/catch__ combined with __axios__ to catch data.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilvestrevivo%2Freact-mobx-async","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsilvestrevivo%2Freact-mobx-async","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsilvestrevivo%2Freact-mobx-async/lists"}