运行 request.post 在节点 js 应用程序中异步

Run request.post asynchronously in node js application

我知道以前有很多类似的问题,但我已经尝试了每一个问题,但每次尝试仍然遇到这个特定问题。所以我正在做的是我有一个名为 data.js 的文件,其中包含一个函数,该函数从 Web api 和 return Json 解析的字符串中获取 json 数据。我有另一个调用此函数的文件,我只是想在控制台上调试结果。但是每次我 运行 我在控制台上得到 'undefined' 的函数,即代码是异步 运行ning 并且值甚至在它被获取之前就得到 returned。 这是我的代码:

data.js

module.exports = {

    holidays: function(x,y,z)
    {

        function intialize()
        {

            return new Promise(function(resolve, reject) {
                    Request.post({
                    "headers": { "content-type": "application/json" },
                    "url": //someurl,
                    "body": JSON.stringify({//some input})
                }, function(err, resp, body) {
                    if (err)
                    {
                        reject(err);
                    } 
                    else
                    {
                        resolve(JSON.parse(body));
                    }
                })
            })

        }

        var Request = require("request");
        var json_content="";
        var req = intialize();

        req.then(function(result) {
            console.log(result);//getting correct answer here
            json_content=result;
            //I know I've to return the value somewhere here but how do i do it
        }, function(err) {
             console.log(err);
        });

        return(json_content);
    }
};

调用函数中的代码:

var h= require(__dirname+'/data.js');
console.dir(h.holidays('x','y','z'));//getting undefined

很简单。您必须使用回调函数来获取结果。

module.exports = {
  holidays: function(x,y,z,callback){
    function intialize()
    {
      return new Promise(function(resolve, reject) {
        Request.post({
            "headers": { "content-type": "application/json" },
            "url": //someurl,
            "body": JSON.stringify({ })
        },
          function(err, resp, body) {
            if (err)
            {
                reject(err);
            }
            else
            {
                resolve(JSON.parse(body));
            }
            })
        })
    }
    var Request = require("request");
    var json_content="";
    var req = intialize();

    req.then(function(result) {
        console.log(result);
        json_content=result;
        callback(json_content); // sending response from here via callback variable
    }, function(err) {
         console.log(err);
    });
        return(json_content);
  }
};

你必须得到这样的回应

var h= require(__dirname+'/data.js');
h.holidays('x','y','z',function(res){
    console.log(res);
})
module.exports = {

    holidays: function(x,y,z)
    {

        function intialize()
        {

            return new Promise(function(resolve, reject) {
                    Request.post({
                    "headers": { "content-type": "application/json" },
                    "url": //someurl,
                    "body": JSON.stringify({//some input})
                }, function(err, resp, body) {
                    if (err)
                    {
                        reject(err);
                    } 
                    else
                    {
                        resolve(JSON.parse(body));
                    }
                })
            })

          }
        }
        var Request = require("request");
        var json_content="";

        return new Promise(function(resolve, reject) {     
           intialize()
          .then(function(result) { 
                 resolve(result);
          })
          .catch(function(err) {
            reject(err);
          });
        });
};

你必须得到这样的回应

var h= require(__dirname+'/data.js');
h.holidays('x','y','z').then(function(res) {
    console.log(res)
})