我是 Javascript 新手,我对下面的代码感到困惑了几个小时。 我想知道为什么我不能将结果的值分配给 htmlContent。 谢谢!
var htmlContent = fs.readFileSync(program.file);
restler.get('http://google.com').on('complete', function(result) {
if (result instanceof Error) {
util.puts('Error: ' + result.message);
} else {
htmlContent = result;
}
});
console.log(htmlContent);
最佳答案
这里的问题是,以 reSTLer.get
开头的行在 console.log
之前开始,但不一定在它之前结束。
您应该将 console.log
放入 reSTLer.get
调用中。
关于javascript - 无法更改 javascript 函数中的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17872354/