javascriptnode.jsnode-request

Making a function that yields until a HTTP request finishes


I can be considered new to Node.JS so apologies, in here I am trying to make a function that yields code execution until the code finished making a HTTP request (using the "request" npm module) which then will be returned, the issue is that the library does not yield the code execution, I have tried using promise but it still won't yield code execution.

Original code:

const request = require("request")

// CONFIG
const ROBLOSECURITY = ""
var http_header = {
    "Cookie": ".ROBLOSECURITY="+ROBLOSECURITY
}

function MakeRbxReq(http_method, url, payload) {
    var jsonbody
    var retfunc = {}
    try {
        jsonbody = JSON.stringify(payload)
    } finally {}
    var options = {
        uri: "http://" + url,
        body: jsonbody || "",
        methpd: http_method,
        headers: http_header
    }

    request(options, function(_, res) {
        if (http_method.toUpperCase() == "POST" || http_method.toUpperCase() == "PUT" || http_method.toUpperCase() == "PATCH" || http_method.toUpperCase() == "DELETE") {
            if (res.headers["X-CSRF-TOKEN"]) {
                http_header["X-CSRF-TOKEN"] = res.headers["X-CSRF-TOKEN"]
                options["headers"] = http_header
                if (res.statusCode == 403) {
                    request(options, function(_, res) {
                        retfunc = {statusCode: res.statusCode, body: res.body}
                    })
                } else {
                    retfunc = {statusCode: res.statusCode, body: res.body}
                }
            }
        }
        retfunc = {
            statusCode: res.statusCode,
            body: res.body
        }
        return
    })

    return retfunc
}

console.log(MakeRbxReq("GET", "search.roblox.com/catalog/json?CatalogContext=2&Subcategory=6&SortType=3&SortAggregation=5&Category=6"))

Promise attempt:

const request = require("request")

// CONFIG
const ROBLOSECURITY = ""
var http_header = {
    "Cookie": ".ROBLOSECURITY="+ROBLOSECURITY
}

function MakeRbxReq(http_method, url, payload) {
    var jsonbody
    var retfunc = {}
    try {
        jsonbody = JSON.stringify(payload)
    } finally {}
    var options = {
        uri: "http://" + url,
        body: jsonbody || "",
        methpd: http_method,
        headers: http_header
    }

    async function req() {
        let reqPromise = new Promise(function(resolve, reject) {
            request(options, function(err, res) {
                console.log("resolving")
                resolve({statusCode: res.statusCode, body: res.body})
            })
        })
    }

    req()

    return retfunc
}

console.log(MakeRbxReq("GET", "search.roblox.com/catalog/json?CatalogContext=2&Subcategory=6&SortType=3&SortAggregation=5&Category=6"))

Output from using promise:

C:\Program Files\nodejs\node.exe .\index.js
{}
resolving

Solution

  • request (promise) is asynchronous.

    You should work with await or then

    Here are some examples