【问题标题】:Making a function that yields until a HTTP request finishes创建一个在 HTTP 请求完成之前产生的函数
【发布时间】:2021-10-07 03:40:31
【问题描述】:

我可以被认为是 Node.JS 的新手,所以很抱歉,在这里我试图创建一个函数来执行代码,直到代码完成发出 HTTP 请求(使用“请求”npm 模块),然后将返回该请求,问题是库没有产生代码执行,我尝试过使用promise但它仍然不会产生代码执行。

原码:

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"))

承诺尝试:

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"))

使用 promise 的输出:

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

【问题讨论】:

    标签: javascript node.js node-request


    【解决方案1】:

    request (promise)asynchronous

    您应该使用awaitthen

    Here 是一些例子

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-24
      • 2017-09-12
      • 2019-09-15
      • 1970-01-01
      • 2013-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多