skip to Main Content

When i call my function with a returning

export function getChampion(id, country) {
    $.ajax({ 
        type: "GET",
        url: `http://ddragon.leagueoflegends.com/cdn/9.24.2/data/${country}/champion.json`,
        dataType: "json",
        success: function(data) {
            console.log(data);
            idToChampion(data, id);
        }
    });
}

// Return the ChampionId
function idToChampion(data, theId) {
    let resultObject = search(theId, data);
    console.log(resultObject);
    return resultObject.id;
}

function search(key, inputArray) {
    inputArray = Object.values(inputArray.data)
    for (let i = 0; i < inputArray.length; i++) {
        if (inputArray[i].key === key) {
            return inputArray[i];
        }
    }
}

The function “getChampion” return undefined i don’t understand why and how fix it. Thank by advance

2

Answers


  1. If you don’t want it to return undefined, you can use a Promise to make it an async function.

    export function getChampion(id, country) {
        return new Promise(resolve => {
            $.ajax({ 
                type: "GET",
                url: `http://ddragon.leagueoflegends.com/cdn/9.24.2/data/${country}/champion.json`,
                dataType: "json",
                success: function(data) {
                    resolve(data)
                }
            });
        })
    }
    

    And then you can use

    let data = await getChampion(id, country);
    ...
    
    Login or Signup to reply.
  2. Your getChampion function doesn’t return any value.

    Assuming you want to get champion data based on their id, you could try this function:

    async function getChampion({id, lang = "en_US"}) {
      const {data} = await $.getJSON(`http://ddragon.leagueoflegends.com/cdn/9.24.2/data/${lang}/champion.json`).promise()
      return data[id]
    }
    

    Here’s how to call it:

    (async () => {
      console.log(await getChampion("Aatrox"))
      //=> { "title": "the Darkin Blade", "blurb": "Once honored defenders of Shurima against the Void, Aatrox and his brethren would eventually become an even greater threat to Runeterra, and were defeated only by cunning mortal sorcery. But after centuries of imprisonment, Aatrox was the first to find...", ... }
    
      console.log(await getChampion("Ahri"))
      //=> { "title": "the Nine-Tailed Fox", "blurb": "Innately connected to the latent power of Runeterra, Ahri is a vastaya who can reshape magic into orbs of raw energy. She revels in toying with her prey by manipulating their emotions before devouring their life essence. Despite her predatory nature...", ... }
    })()
    

    You can also use the promise interface:

    getChampion("Aatrox").then(console.log)
    //=> { "title": "the Darkin Blade", "blurb": "Once honored defenders of Shurima against the Void, Aatrox and his brethren would eventually become an even greater threat to Runeterra, and were defeated only by cunning mortal sorcery. But after centuries of imprisonment, Aatrox was the first to find...", ... }
    
    getChampion("Ahri").then(console.log)
    //=> { "title": "the Nine-Tailed Fox", "blurb": "Innately connected to the latent power of Runeterra, Ahri is a vastaya who can reshape magic into orbs of raw energy. She revels in toying with her prey by manipulating their emotions before devouring their life essence. Despite her predatory nature...", ... }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search