将request.post中的body(html)响应保存/消费到nodejs的全局变量中

我希望将body的内容保存到我的全局变量returnData中,这样我就可以将它返回给外部函数。但是只有当我在回调中console.log它的时候,它才能工作。数据不会保存到全局变量中。

    var returnData; // This is where I want to store the output of request.post


    function getDataFromPostRequest() {
      request.post(
        {
          url: "http://example.com",
          formData: {x: "y"},
        },
        function optionalCallback(err, httpResponse, body) {
          console.log(body) // this is working
          returnData = body;

        }
      );
    }

    console.log("returnData ", returnData); // This is undefined

转载请注明出处:http://www.shenzhenzsmy.com/article/20230526/1323148.html