0

I am creating a twitch chat bot with node. I need to get a list of moderators from the link http://tmi.twitch.tv/group/user/:channel/chatters I can recieve the data and parse it, but I just need to know how to return the data within the requests and save it into a variable.Here is what I have so far.

function getMods(person, chanel) {
    var channel = getUser(chanel);

    request('http://tmi.twitch.tv/group/user/' + channel + '/chatters',     function(error, response, body) {
    if (!error && response.statusCode == 200) {
      var mods = JSON.parse(body);
      // I want to return the value mods through the function
  }
  });
}

So I can call the function and store the array such as this.

var mods = getMods('xtreameprogram2', '#zero_optics');

Is it possible without using callbacks?

Ford Smith
  • 195
  • 1
  • 1
  • 7
  • You can't return the value - `getMods()` has already returned before the value is even available. You must use the response in the callback and any code that you want to be executed after the response is received must actually be in the callback. This is how you program with asynchronous responses. It is different than synchronous programming. Many examples provided in the duplicate. – jfriend00 Jan 22 '16 at 01:23
  • That is what I was afraid. Thanks anyways. – Ford Smith Jan 22 '16 at 02:53
  • There are plenty of ways to solve your problem, just not with plain synchronous coding. If you're going to be writing node.js code, you will need to spend some time learning how to write asynchronous code. Beyond the core principles using callbacks, I'd suggest learning how to use promises as they can simplify the coordination and error handling especially when dealing with multiple asynchronous operations and when sequencing multiple operations. – jfriend00 Jan 22 '16 at 02:58

0 Answers0