• If you need help or want to discuss things, you now can also join us on our Discord Server!
  • A first preview of the unlimited version of SinusBot can be found in the Upcoming Changes thread. A version for Windows will follow, but we don't have a release date, yet.

EN http request

Montiary

Active Member
Contributor
hey, i am actually rly new to java script and i have this problem.

i want to fetch some http and want to print it outsite of the request, but it dosnt work.

can anyone help me with that?

Code:
    async function getSummonerByName(name) {
        var url = "";
        var test;
            await http.simpleRequest({
        'method': 'GET',
        'url': url,
        'timeout': 6000,
    }, function (error, response) {
        if (error) {
            engine.log("Error: " + error);
            return;
        }
        
        if (response.statusCode != 200) {
            engine.log("HTTP Error: " + response.status);
            return;
        }
        
        // success!
        test = response.data.toString();
    });
    engine.log(test);
    }
 

Multivitamin

Well-Known Member
Tier III
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
It looks like you dont know how Promises and asynchronous requests work

JavaScript:
async function getSummonerByName(name) {
  var url = "";
  var test;
  await http.simpleRequest({  //http simplerequest is not a promise function
    'method': 'GET',
    'url': url,
    'timeout': 6000,
  }, function (error, response) { //<- this function will be called AFTER the request finnished which is generally ~200-300ms later
    if (error) {
      engine.log("Error: " + error);
      return;
    }
    if (response.statusCode != 200) {
      engine.log("HTTP Error: " + response.status);
      return;
    }
    // success!
    test = response.data.toString();
  });
  engine.log(test); //this will be executed before the callback (2nd paramter of http.simpleRequest)
}
 

Montiary

Active Member
Contributor
i know what async is but i thought with await it would be waiting for it till its done
 

Montiary

Active Member
Contributor
It looks like you dont know how Promises and asynchronous requests work

JavaScript:
async function getSummonerByName(name) {
  var url = "";
  var test;
  await http.simpleRequest({  //http simplerequest is not a promise function
    'method': 'GET',
    'url': url,
    'timeout': 6000,
  }, function (error, response) { //<- this function will be called AFTER the request finnished which is generally ~200-300ms later
    if (error) {
      engine.log("Error: " + error);
      return;
    }
    if (response.statusCode != 200) {
      engine.log("HTTP Error: " + response.status);
      return;
    }
    // success!
    test = response.data.toString();
  });
  engine.log(test); //this will be executed before the callback (2nd paramter of http.simpleRequest)
}


ahhh know i know what do you want to teach me :)
solved my problem thx

#close
 
Top