{"id":22830961,"url":"https://github.com/eyalgolan/async-api-fetcher","last_synced_at":"2026-02-02T07:37:45.547Z","repository":{"id":256200274,"uuid":"854582484","full_name":"eyalgolan/async-api-fetcher","owner":"eyalgolan","description":null,"archived":false,"fork":false,"pushed_at":"2024-09-09T12:43:05.000Z","size":462,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-03T20:41:29.324Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":null,"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/eyalgolan.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":"2024-09-09T12:38:13.000Z","updated_at":"2024-09-09T12:43:08.000Z","dependencies_parsed_at":"2024-09-09T15:21:33.689Z","dependency_job_id":"c5f365c1-fef0-40ad-b80a-0ca772431113","html_url":"https://github.com/eyalgolan/async-api-fetcher","commit_stats":null,"previous_names":["eyalgolan/async-api-fetcher"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/eyalgolan/async-api-fetcher","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyalgolan%2Fasync-api-fetcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyalgolan%2Fasync-api-fetcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyalgolan%2Fasync-api-fetcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyalgolan%2Fasync-api-fetcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eyalgolan","download_url":"https://codeload.github.com/eyalgolan/async-api-fetcher/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eyalgolan%2Fasync-api-fetcher/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29007312,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-02T06:37:10.400Z","status":"ssl_error","status_checked_at":"2026-02-02T06:37:09.383Z","response_time":58,"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":[],"created_at":"2024-12-12T20:16:10.937Z","updated_at":"2026-02-02T07:37:45.520Z","avatar_url":"https://github.com/eyalgolan.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Async API Fetcher\n\nThis project demonstrates a common issue in JavaScript related to handling asynchronous operations and race conditions. It fetches data from a public API but contains a subtle bug caused by incorrect usage of `async/await` and `forEach`, which results in an empty or partially filled data array. The project is useful for understanding and fixing race conditions in Node.js.\n\n## Table of Contents\n\n- [Introduction](#introduction)\n- [Setup](#setup)\n- [Usage](#usage)\n- [Problem Overview](#problem-overview)\n- [Solution](#solution)\n- [License](#license)\n\n## Introduction\n\nIn this project, we attempt to fetch data from multiple public APIs asynchronously and process the data. However, due to improper handling of asynchronous loops, the program doesn’t work as expected, and the issue can be difficult to debug. The project is designed to be simple but highlights an important pitfall in JavaScript's asynchronous programming model.\n\n## Setup\n\nFollow these steps to set up and run the project locally:\n\n1. Clone or download the repository:\n   ```bash\n   git clone https://github.com/your-username/async-api-fetcher.git\n   cd async-api-fetcher\n   ```\n\n2. Initialize the project and install dependencies:\n   ```bash\n   npm install\n   ```\n\n   This will install the only required dependency, `axios`, which is used to make HTTP requests.\n\n3. You are now ready to run the project.\n\n## Usage\n\n1. To run the project, simply execute the following command in your terminal:\n   ```bash\n   node index.js\n   ```\n\n2. The program will attempt to fetch data from several public APIs, simulate some processing time for each API call, and then log the processed data.\n\n   However, due to an issue in the asynchronous handling of API calls, the data array will often be empty or incomplete.\n\n## Problem Overview\n\nThe issue with the current implementation lies in how the asynchronous operations are handled. The code uses `forEach` with `async/await`, but `forEach` does not work well with promises because it doesn't wait for each iteration to complete before proceeding.\n\n### Problem Code (in `index.js`)\n\n```javascript\nasync function processApiData() {\n  const dataResults = [];\n\n  apiUrls.forEach(async (url) =\u003e {\n    await new Promise((resolve) =\u003e setTimeout(resolve, 1000));\n    const data = await fetchDataFromApi(url);\n    dataResults.push(data);\n  });\n\n  console.log('Processed data:', dataResults);\n}\n```\n\nIn this code, `dataResults.push(data)` is executed before the promises resolve, resulting in an incomplete or empty array when the data is logged.\n\n### Expected Output\n\nThe expected output should be an array of data fetched from the APIs, but instead, the output will typically be an empty array.\n\n## Solution\n\nTo fix the issue, we can replace the `forEach` loop with `Promise.all` and properly handle the asynchronous calls.\n\n### Fixed Code\n\n```javascript\nasync function processApiData() {\n  const promises = apiUrls.map(async (url) =\u003e {\n    await new Promise((resolve) =\u003e setTimeout(resolve, 1000));\n    return fetchDataFromApi(url);\n  });\n\n  const dataResults = await Promise.all(promises);\n  console.log('Processed data:', dataResults);\n}\n```\n\n### Running the Fixed Code\n\nOnce you replace the problematic code with the solution above, running the program again will produce the correct output, with all API data properly fetched and logged.\n\n## License\n\nThis project is open-source and available under the [MIT License](LICENSE).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feyalgolan%2Fasync-api-fetcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feyalgolan%2Fasync-api-fetcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feyalgolan%2Fasync-api-fetcher/lists"}