1

在 node.js 中,我deasync用来返回request查询结果。

var request = require('request');
var deasync = require("deasync");

const URL_1 = "...", URL_2 = "...", USER_AGENT = "...";

function getHtml() {
  function requrl(url, form) {
    return {
      url: url,
      headers: {'User-Agent': USER_AGENT},
      form: form,
      jar: true
    }
  }
  var form = {email:"email@geemail.com", password:"thepass"};
  var resp;

  request.post(requrl(URL_1, form), function (error, response, body) {
    if (error || response.statusCode != 200) { 
      resp = error;
    } else {
      request(requrl(URL_2), function (error, response, body) {
        console.log(response); // <- this makes it work
        if (error || response.statusCode != 200)
          resp = error;
        else
          resp = body;
      })
    }
  })

  while (resp === undefined) deasync.runLoopOnce();
  return resp;
}

这工作正常。但如果我删除这一行:console.log(response);它永远不会返回。我不知道发生了什么。

4

0 回答 0