JavaScript,使用Promises同时发出POST和GET请求

问题描述 投票:3回答:1

背景:我有一个需要一个小调试的函数,它与API请求(POST,GET)和promises有关。在过去的几天里,我一直试图弄清楚异步函数是如何工作的,结果发现直接将这些函数的值返回到同步函数是不可能的(或者我听说过)。

我最初的想法是创建一个POST请求函数,该函数返回一个访问令牌,我可以将其插入我的GET函数的授权头(只要提供令牌,它就能正常工作)。但是,从我的发现来看,我无法在两个单独的方法上执行此操作,因为POST请求在函数外部返回undefined。所以我现在的想法是在POST中包含GET请求,以便可以直接传递访问令牌而无需任何方法调用。我尝试使用Promises来做到这一点,我不是很熟悉,但设法有所帮助。我的期望是我可以调用httpGetData()函数,它首先获得令牌,然后将其传递给.then()部分,该部分应该返回实际数据或[object Promise]而不是undefined。

问题:问题在于,当我调用httpGetData()函数时,它返回undefined,但我的console.log(result[0])清楚地表明GET请求已经获取了我想要的数据。我想知道是否有一个调整允许我的代码返回这些数据(console.log对我来说没用,除了看看在api请求的背景上发生了什么)。通过返回.then()中的值,我看到了一些像这些问题的回答,但我的尝试都返回undefined。

我附上了输出的截图。请记住,这是一个Alexa技能Lambda代码,所以返回出现在.speak()console.log后面。

function httpGetData() {

  var promise1 = new Promise(((resolve, reject) => {
    var options = {
      method: "POST", // POST request intended to get access token
      host: 'hostinfo',
      path: "pathinfo",                                                        
      headers: {
        "content-type": "stuff here",
        "cache-control": "no-cache",
     }
  };

  var request = http.request(options, function (response) {
    var returnData = [];

    response.on("data", function (chunk) {
      returnData += chunk;
    });

    response.on("end", function () {
      var data = JSON.parse(returnData);
      var accessToken = data.access_token; // the access token value of the POST api request like "039509544932n3249fc21232nc32423nci"
      resolve(accessToken);
    });

    response.on('error', (error) => {
      reject(error);
    });
  });

  request.write("------stuff: form-data; name=\"client_secret\"\r\n\r\stuff here\r\n------stuff: form-data; name=\"client_id\"\r\n\r\stuff here\r\n------stuff: form-data; name=\"grant_type\"\r\n\r\nclient_credentials\r\n------stuff");
  request.end();

  }));

  promise1.then(function(value) {
      return new Promise(((resolve, reject) => {
        var options = {
          method: "GET", //this is a GET request so it's intended to get usable data using access token
          host: 'hostinfo',
          path: "pathinfo",                
          headers: {
            "content-type": "stuff here",
            "Authorization": "Bearer " + value, // value is the access token that was resolved earlier ("039509544932n3249fc21232nc32423nci") so I'm trying to concatonate this to the authorization header
            "cache-control": "no-cache",
         }
    };

      var request = http.request(options, function (response) {
        var returnData = [];

        response.on("data", function (chunk) {
          returnData += chunk;
        });

        response.on("end", function () {
          console.log(value); //this is the line that prints access token in screenshot
          resolve(JSON.parse(returnData));
        });

        response.on('error', (error) => {
          reject(error);
        });
      });

      request.write("------stuff: form-data; name=\"client_secret\"\r\n\r\stuff here\r\n------stuff: form-data; name=\"client_id\"\r\n\r\stuff here\r\n------stuff: form-data; name=\"grant_type\"\r\n\r\nclient_credentials\r\n------stuff");
      request.end();

        }))
  }).then((result) => {
            console.log('hello');
            return result; //I'm not sure here but wanted to return the result 
            }).then((result) => {
                  console.log(result[0]); // just a check which shows that result[0] actually executes and can be printed as shown in screenshot
                  return result[0]; //other desperate attempt


});


}// httpGetData() function ends here

return image

Console log image

javascript node.js post promise get
1个回答
0
投票

由于@Bergi,我终于解决了这个问题。虽然可以缩短这个答案,但我想发布它以便它可以作为一个选项。 Bergi建议的是,不是在一个函数中完成POST和GET请求,我们可以将它们分开并使用另一个函数同时执行它们。在下面的代码中,httpToken()执行POST请求并返回访问令牌。 httpResource(accessToken)函数执行GET请求,它将获取accessToken参数。另一个函数finalData()将帮助我们同时发出POST和GET请求,从而返回一个[object Promise]而不是undefined,我们可以访问它们。我们的最后一步是使用async / await函数(retrieveInfo()),它将使用finalData()从我们的请求中获取响应。请享用!

function httpToken() {
  return new Promise(((resolve, reject) => {
    var options = {
      method: "POST",
      host: 'hostinfo',
      path: "pathinfo",                                                        
      headers: {
        "content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
        "cache-control": "no-cache",
     }
  };

  var request = http.request(options, function (response) {
    var returnData = [];

    response.on("data", function (chunk) {
      returnData += chunk;
    });

    response.on("end", function () {
      var data = JSON.parse(returnData);
      var accessToken = data.access_token;
      resolve(accessToken); //resolving access token (can be changed to resolve(data) to include timestamp etc)
    });

    response.on('error', (error) => {
      reject(error);
    });
  });

  request.write("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_secret\"\r\n\r\n"client secret here"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_id\"\r\n\r\n"client id here"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"grant_type\"\r\n\r\n"grant type here"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
  request.end();

  }));
}


function httpResource(accessToken){
      return new Promise(((resolve, reject) => {
        var options = {
          method: "GET",
          host: 'hostinfo',
          path: "pathinfo",                                                       
          headers: {
            "content-type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
            "Authorization": "Bearer " + accessToken,
            "cache-control": "no-cache",
         }
    };

      var request = http.request(options, function (response) {
        var returnData = [];

        response.on("data", function (chunk) {
          returnData += chunk;
        });

        response.on("end", function () {
          resolve(JSON.parse(returnData));
        });

        response.on('error', (error) => {
          reject(error);
        });
      });

      request.write("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_secret\"\r\n\r\n"client secret here"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"client_id\"\r\n\r\n"client id here"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"grant_type\"\r\n\r\n"grant type here"\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--");
      request.end();


    }));
}

function finalData()
{
  return httpToken().then(token => httpResource(token));
}

async function retrieveInfo()
{
    response = await finalData(); //response will have the data returned from our GET request using finalData()

    //do stuff with response...
}
© www.soinside.com 2019 - 2024. All rights reserved.