Return ID - Function

Hey, I just want to write a function which gives the ID of the Stream back with return but everything I try, i cant return the ID. Maybe a logic mistake of myself …

Name = Streamname
channelID = holds the ID

function live(Name) {
var clientID_ = "XYZ";
var channelID = 0;

$.ajax({
method: "GET",
url: "https://api.twitch.tv/kraken/users/" + Name + "?client_id=" + clientID_,
}).done(function(channel) {
channelID = channel._id;
})

 return ID;
}

First glance,

return ID;

you mean:

return channelID;

?

y, but it changed nothing. it seems, i cant fill the variable channelID with channel._id

He just ignore the fill and always the 0 from the start.

…annnnnd second glance:

$.ajax is asyncronus, so when the return is exectuted, you probably havn’t gotten a response from the api yet. You need to do it with a callback.

So, for a concept:

function live(Name, callback) {
var clientID_ = "XYZ";
var channelID = 0;

$.ajax({
method: "GET",
url: "https://api.twitch.tv/kraken/users/" + Name + "?client_id=" + clientID_,
}).done(function(channel) {
callback(channel._id);
})
}

Then you’d call it like:

live ("some name", function (id) {
    console.log(id);
});

But remember again, ajax is async, so your program will carry on doing whatever after it and the callback function will be executed once the request has been completed.

1 Like
function live(Name) {
  var clientID_ = "XYZ";
  var channelID = 0;

  var response = $.ajax({
    method: "GET",
    url: "https://api.twitch.tv/kraken/users/" + Name + "?client_id=" + clientID_,
    success: function(data){
      return data._id;
    }
  });

  console.log(response);
}

made something like this, but get the hole “object” as callback back in the console.log

The success function returns to jquery’s internal code, not to your response. It’s also executed after your console.log(response).

You cannot get the info via return due to ajax being asynchronous.

hmm so i tried like these (use a function) but wont work too. There are any possiblily to do this?

$('.content').on('click', function() {
  //console.log(data[1].id + " - " + data[1].live + " - " + data[1].name);

  console.log(live("summonersinnlive"));
});

function live(Name) {
  var clientID_ = "XYZ";
  var channelID = 0;

  $.ajax({
    method: "GET",
    url: "https://api.twitch.tv/kraken/users/" + Name + "?client_id=" + clientID_,
    success: function(data){
      handoff(data._id);
    }
  });

  function handoff(id)
  {
    channelID = id;
  }
  
  return channelID;
}

I posted how to do it with a callback higher up…

oh im stupid … rly thanks, it works!

So I’ve got some questions more, sorry guys to annoying you and attention u maybe scream at this code.

Is there any option to save the result/response as object in the array. tried something like these:

function GetData(Name, callback) {
  GetID(Name, function(user) {
    GetStatus(user._id, function(channel) {
		var data = ({
			'time' : time,
			'user' : user,
			'channel' : channel,
			'game' : (channel.stream === null) ? null : function(){
				GetGame(data.channel.stream.game, function(game){
					ausgabe(game.games[0].box.large);
				});
			}
		});
      callback(data);
    });
  });
}

the idea behind this, i want just the completly infos in 1 array of objects and call it with something like these:

GetData("summonersinnlive", function(data){
		ausgabe(data.game);
	});

The problem is like u said i think, the async

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.