array gets printed in random order, when inside axios-get method, but correctly, when outside

Issue

I’ve been struggling with a problem, that thrives the nerves out of me.

I wrote axios GET-Methods, to receive all Minecraft-releases, starting from 1.12.

router.get("/currentReleases", (req, res) => {
    
    get("https://launchermeta.mojang.com/mc/game/version_manifest.json").then((data) => {
        var currentReleases = Array()

        data.versions.forEach(version => {
           const releaseDate = new Date(version.releaseTime);
           const releaseDate_1_12 = new Date("2017-06-02T13:50:27+00:00")

           if(releaseDate_1_12 <= releaseDate) {
            if(version.type == "release") {
                currentReleases.push(version)
            }
           }
        });

        res.send(currentReleases);
    })
});

This piece of code is working as intended.

The second code-piece should filter all available latest paper-spigot releases, of the current version, looping through a forEach:

router.get("/availablePaperReleases", (req,res) => {
    
    get("http://localhost:3001/currentReleases").then((data) => {
        var currentReleases = Array();
        data.forEach((paper) => {
            currentReleases.push(paper.id);
        })
        
        var currentPaperBuilds = Array();

        currentReleases.forEach((version, index, array) => {
            // CORRECT ORDER
            // console.log(version + " : " + index + " : " + array.length)
            get("https://api.papermc.io/v2/projects/paper/versions/"+version+"/builds").then((paperdata) => {

                // RANDOMIZED ORDER
                console.log(version + " : " + index + " : " + array.length)
                
            }).catch(error => console.log("no data"))

            if(index == array.length - 1) {
                console.log("Sending data");
                res.send(currentPaperBuilds);
            }
        })
        
    });
});

the console.log() with the // CORRECT ORDER comment, works flawless. But as soon as the console.log() is inside the get()-method, the output is all weird and completely randomised, every time, I re-run the code.

get-method:

async function get(url) {
    let response = await axios.get(url);
    return response.data
}

Correct order output:

1.19.2 : 0 : 28
1.19.1 : 1 : 28
1.19 : 2 : 28
1.18.2 : 3 : 28
1.18.1 : 4 : 28
1.18 : 5 : 28
1.17.1 : 6 : 28
1.17 : 7 : 28
1.16.5 : 8 : 28
1.16.4 : 9 : 28
1.16.3 : 10 : 28
1.16.2 : 11 : 28
1.16.1 : 12 : 28
1.16 : 13 : 28
1.15.2 : 14 : 28
1.15.1 : 15 : 28
1.15 : 16 : 28
1.14.4 : 17 : 28
1.14.3 : 18 : 28
1.14.2 : 19 : 28
1.14.1 : 20 : 28
1.14 : 21 : 28
1.13.2 : 22 : 28
1.13.1 : 23 : 28
1.13 : 24 : 28
1.12.2 : 25 : 28
1.12.1 : 26 : 28
1.12 : 27 : 28
Sending data
no data

Randomised order output:

Sending data
1.15 : 16 : 28
1.16.3 : 10 : 28
1.14 : 21 : 28
1.15.1 : 15 : 28
1.17 : 7 : 28
1.19 : 2 : 28
1.18.1 : 4 : 28
no data
1.19.2 : 0 : 28
1.19.1 : 1 : 28
1.17.1 : 6 : 28
1.18 : 5 : 28
1.16.2 : 11 : 28
1.14.1 : 20 : 28
1.14.3 : 18 : 28
1.14.2 : 19 : 28
1.16.1 : 12 : 28
1.12.1 : 26 : 28
1.12 : 27 : 28
1.16.4 : 9 : 28
1.14.4 : 17 : 28
1.18.2 : 3 : 28
1.15.2 : 14 : 28
1.12.2 : 25 : 28
1.13 : 24 : 28
1.13.2 : 22 : 28
1.13.1 : 23 : 28
1.16.5 : 8 : 28

The Sending data comment, should be executed at last.

I’d appreciate any tip I can get, to fix this problem.

You can find the complete code here.

Solution

Thanks to a greater rewrite someone did for me, the array is now functioning as it should:

const get = async (url) => {
    return new Promise(async (resolve, reject) => {
        let response = await axios.get(url).catch(reject);
        resolve(response?.data);
    });
};

const getReleases = async (url) => {
    return new Promise(async (resolve, reject) => {
        const release_1_12 = new Date('2017-06-02T13:50:27+00:00');
        let data = await get('https://launchermeta.mojang.com/mc/game/version_manifest.json').catch(reject);
        var currentReleases = data.versions.filter((v) => v.type === 'release' && new Date(v.releaseTime) >= release_1_12);
        resolve(currentReleases);
    });
};

router.get('/currentReleases', async (req, res) => {
    res.json(await getReleases());
});

router.get('/availablePaperReleases', async (req, res) => {
    let releases = await getReleases();
    var currentPaperBuilds = await Promise.all(
        releases.map(async (rel) => {
            let buildData = await get('https://api.papermc.io/v2/projects/paper/versions/' + rel.id + '/builds').catch((err) => {});
            if (buildData) {
                return {
                    ...rel,
                    build: buildData,
                };
            } else {
                return rel;
            }
        })
    );
    res.json(currentPaperBuilds);
});

Answered By – LinuxSquare

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published