Return variables inside http.request?

function http_get (ip, port) {
    let WAN = false;
    //http.ClientRequest
    var data = {  // 
        msg: "can you hear me?",  
        time: new Date().getTime()
    };
    var content = qs.stringify(data);
    var options = {
        host : ip,
        port : port,
        path : "/",
        method : "POST",
        headers: {
            "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
        }
    };
    var request = http.request(options, function(res) {
        console.log("STATUS: " + res.statusCode);
        if (res.statusCode == "200") {
            WAN = true;
        }
        console.log(WAN)  //true
        console.log("HEADERS: " + JSON.stringify(res.headers));  
        res.setEncoding("utf8");
        console.log("get respones");
        let data = "";
        res.on("data", function (chunk) {
            console.log("BODY: " + chunk);
        });
        res.on("end", function (e) {  
            console.log("-----end----")
        });
    });
    request.on("error", function (e) {  
        console.log("problem with request: " + e.message);  
    });
    request.write(content);
    request.end();     //
    return WAN;
}
let a = http_get("127.0.0.1", 3000)
console.log("a:"+ a);  //a:false

print out a:false below and true on top. How can I print out a:true?

Apr.09,2022

change the middle http.request promise or use Async/Await . You can refer to article

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e404e3-42e54.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e404e3-42e54.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?