I’ve been exploring some forums, and I’ve noticed that using the return statement should pass a value back to the parent function. However, the code I’ve shared below consistently returns undefined.
I suspect this might be linked to timing issues in my code, possibly related to sync and async processes (still diving deeper into this topic). Can anyone help identify the mistake in my code and perhaps point me in the right direction? Links to helpful documentation would be appreciated as well.
const tmi = require("tmi.js");
const fetch = require("node-fetch");
const commandComponents = message.toLowerCase().split(" ");
// Trigger the command execution
if (commandComponents[0] === "!shoutout" || commandComponents[0] === "!so") {
console.log(checkUser(commandComponents[1], channel));
}
// Function to check if the user exists
function checkUser(usernameToCheck, channel) {
fetch(
`https://api.twitch.tv/helix/users?login=${usernameToCheck.toLowerCase().trim()}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${O_Token}`,
"Client-Id": C_ID
}
}
)
.then(response => response.json())
.then(data => {
if (data.data !== "undefined") {
fetch(
`https://api.twitch.tv/helix/channels?broadcaster_id=${data.data[0].id}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${O_Token}`,
"Client-Id": C_ID
}
}
)
.then(result => result.json())
.then(channelData => {
return data.data[0].game_name;
});
}
});
}