`var req = http.request(options, function(res)
{
hh=res.headers["last-modified"]; /* hh is global variable */
});req.end();
/*print the last modified time of file that stored in hh
console.log(hh);
如何在获取req.end后的hh值后使用hh?
答案 0 :(得分:0)
请做一些关于异步代码的教程。您不了解执行顺序,我已在下面突出显示。
//this line executes at time T1.1
var req = http.request(options, function(res) {
//This line executes at time T2.1, AFTER T1.1 and T1.2
hh=res.headers["last-modified"]; /* hh is global variable */
//This line executes at time T2.2 and hh IS AVAILABLE NOW
console.log(hh);
});
//This line executes at time T1.2
//hh is NOT SET YET. CANNOT USE IT HERE. This is what async means.
req.end();