findissues icon indicating copy to clipboard operation
findissues copied to clipboard

Inefficient API Handling in loadActiveRepoDetails.js

Open codeCraft-Ritik opened this issue 10 months ago • 0 comments

Summary:

The current implementation uses repos_list.map with an async callback but returns the array of promises immediately without waiting for them to resolve. This results in the function returning an array of pending Promise objects rather than the actual repository data.

Fix: Use Promise.all() to wait for all concurrent API requests to finish before returning the result.


Corrected Code `(findissues/pages/api/loadActiveRepoDetails.js)`:

export default async function loadActiveRepoDetails(repos_list) {
    const repo_promises = repos_list.map(async (repo_url) => {
        const repo_name = repo_url.split("/")[3] + "/" + repo_url.split("/")[4];
        const repo_endpoint = 'https://api.github.com/repos/' + repo_name;

        const response = await fetch(repo_endpoint, {
            headers: {
                Authorization: "token " + process.env.NEXT_PUBLIC_FETCH_REPO,
                Accept: "application/vnd.github.v3+json",
            },
        });

        // Check if response is successful (Related to Issue #2)
        if (!response.ok) return null;

        const result = await response.json();
        return {
            full_name: result.full_name,
            updated_at: result.updated_at,
            stars: result.stargazers_count,
            forks: result.forks_count,
            language: result.language,
            open_issues: result.open_issues_count,
            issue_url: repo_endpoint + '/issues'
        };
    });

    // Wait for all promises to resolve and filter out failed requests
    const results = await Promise.all(repo_promises);
    return results.filter(repo => repo !== null);
}

codeCraft-Ritik avatar Jan 23 '26 08:01 codeCraft-Ritik