How can I keep a permanent variable out of req.on scope on a Node.JS request.get request?

I’m able to make a GET request and retrieve some data I want, But I can’t actually get the data where I need it. Everytime I try to assign it to a variable it’s undefined. I’m trying to do it using the request = require(‘https’) + request.get(url, res) + res.on(‘end’, callback) structure.

When I wrap a callback function around req.on he just retrieves me the req object, which is not what I want. I want the result from my callback function.

Here is a sample of what I’m trying to do:

let request = require("https")

    treatedQuery = query.replaceAll(" ","+")
    console.log(treatedQuery)
    url = `https://www.youtube.com/results?search_query=${treatedQuery}+trailer`
    let someVariable = ""

    request.get(url, res => {
        let data = ''

        res.on('data', chunk => {
            data += chunk;
        });

        function someCallback(something) {
            return something
        }

        someVariable = someCallback(res.on('end', () => {
            regex = //watch?v=(.{11})/g
            linkDump = data.match(regex)
            console.log(linkDump[0])
            return linkDump[0]
        }));

    }).on('error', err => {
            console.log('Error: ', err.message);
        });
    
    return someVariable
}

I intend to hand the results of this get to a front-end so a user can click on the link based on the string he passed. I don’t know if this is efficient or if there’s some xyz module or library that I should be using, but I’d rather answers were kept in this scope if possible. Suggesting a completely different approach of some other library/framework will only increase the frustration and anxiety.

I know there are MANY answers about this topic on SO, I’ve especially read the one by Felix How do I return the response from an asynchronous call?, but his solution simply does not apply. That’s because I can’t run what I want inside the callback function, can’t run req.on directly inside the callback and can’t run the inner function on callback either because it just runs before req.on and as such is empty.

Any help is appreciated