• If you need help or want to discuss things, you now can also join us on our Discord Server!

EN http request

Montiary

Donor
is awesome!
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
V.I.P.
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

Donor
is awesome!
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
 

Similar threads

Top