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
If you don’t want it to return undefined, you can use a Promise to make it an async function.
And then you can use
Your
getChampion
function doesn’t return any value.Assuming you want to get champion data based on their id, you could try this function:
Here’s how to call it:
You can also use the promise interface: