鉴于以下示例,为什么outerScopeVar在所有情况下都未定义?
var outerScopeVar;
var img = document.createElement('img');
img.onload = function() {
outerScopeVar = this.width;
};
img.src = 'lolcat.png';
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
var outerScopeVar;
setTimeout(function() {
outerScopeVar = 'Hello Asynchronous World!';
}, 0);
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
// Example using some jQuery
var outerScopeVar;
$.post('loldog', function(response) {
outerScopeVar = response;
});
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
// Node.js example
var outerScopeVar;
fs.readFile('./catdog.html', function(err, data) {
outerScopeVar = data;
});
console.log(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
// with promises
var outerScopeVar;
myPromise.then(function (response) {
outerScopeVar = response;
});
console.log(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)
// geolocation API
var outerScopeVar; …Run Code Online (Sandbox Code Playgroud) 我对这个主题有一个问题: bcrypt.compare() 是异步的,这是否一定意味着延迟肯定会发生?
由于我的会员级别不允许我发表评论,所以我不得不打开新主题。
我的问题是有什么缺点,或者是否有任何缺点可以使用bcrypt.compareSync()而不是异步版本bcrypt.compare()。
compareSync()绝对给出了正确的结果。那么为什么不使用它并使用compare()包装在 Promise 中的呢?它会阻止 NodeJS 为其他用户提供服务吗?