我很好奇Event Loop和Promise之间的关系.
该演示揭示了这个问题.我希望它p1 fulfilled出现在中间,因为它们将任务排队到同一个任务队列并逐个执行.
var p1 = new Promise(function(resolve, reject){
resolve(1)
})
setTimeout(function(){
console.log("will be executed at the top of the next Event Loop")
},0)
p1.then(function(value){
console.log("p1 fulfilled")
})
setTimeout(function(){
console.log("will be executed at the bottom of the next Event Loop")
},0)
Run Code Online (Sandbox Code Playgroud)
控制台结果是:
p1 fulfilled
will be executed at the top of the next Event Loop
will be executed at the bottom of the next Event Loop
Run Code Online (Sandbox Code Playgroud)
可视化效果显示promise.then回调没有进入事件循环的任务队列.这是正确的?
【注意:问题与Promise vs setTimeout不一样,因为它更关注Event Loop和Promise之间的关系】
相关问题告诉我,解决一个新的承诺,即使它立即得到解决,它也被安排在下一个循环中。不过,评论部分似乎是反例。
var p1 = Promise.resolve("p1")
/* console order will be "p2" "p1" */
// pattern1
// var p2 = Promise.resolve(Promise.resolve("p2"));
// pattern2
// var p2 = Promise.resolve(new Promise(function(resolve, reject){
// resolve("p2")
// }))
/* console order will be "p1" "p2" */
// pattern3
var p2 = new Promise(function(resolve, reject){
resolve(Promise.resolve("p2"));
})
p2.then(function(value){
console.log(value);
})
p1.then(function(value){
console.log(value);
})
Run Code Online (Sandbox Code Playgroud)
在 Chrome v61.0.3163.91 中很奇怪,但在 Node.js 中正常。
注意:这个问题与流行的问题不一样。只需关注顺序问题:为什么不同的模式会产生不同的顺序?首选事件循环方面的解释。
我在一个项目中收到了一个新要求,我们必须阻止Windows' alt+ tab热键来阻止窗口之间的切换.
很多的努力后,我可以预防alt,ctrl,tab,shift,ctrl+ s,ctrl+ c,ctrl+ v等,但我不能阻止alt+ tab,无论是Firefox或Chrome.
我在MDN中搜索并最终发现:阻止Chrome中相应按键事件的默认操作
所以我的猜测是alt+ tab是Windows系统热键而不是浏览器.并且event.preventDefault()只能阻止与浏览器对应的事件.
有没有人有更详细的解释?
以下是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
document.addEventListener("keydown", function(e) {
//tab keyCode===9
//I hope to prevent alt+tab event action in windows
if (e.altKey && e.keyCode === 9) {
e.preventDefault(); //why not come in?
}
}, …Run Code Online (Sandbox Code Playgroud) 我在这里找到了关于API的解释,它告诉我第二个参数是一个字符串.
它在firefox中正常运行.但是,在chrome中,它显示以下错误:
我只是好奇这是什么原因造成的?是因为api仍处于工作草案状态,不同的浏览器会执行不同的实现吗?
以下是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<div id="div">
<ul>
<li>xxx</li>
<li>xxx</li>
<li>xxx</li>
<li>xxx</li>
<li>xxx</li>
</ul>
</div>
<script>
//normal
// var pTag=document.createElement("p");
// div.insertAdjacentElement("beforeend",pTag);
//throw error:
// Uncaught TypeError: Failed to execute 'insertAdjacentElement' on 'Element': parameter 2 is not of type 'Element'.
var txt = "<p>testtest</p>";
div.insertAdjacentElement("beforeend",txt);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我在下面写了一个演示来演示这个问题.
我希望Generator a会对变量进行迭代,然而,影响它后的冗余代码.
任何人都可以一步一步地说清楚为什么会这样吗?
我的代码是:
function* foo(){
var a = 0, c
while(a < 10){
a += 1
yield a //if the semicolon is added here, the final result is 1
[c] = [101]
}
}
var gen= foo()
console.log(gen.next().value) // [101]
Run Code Online (Sandbox Code Playgroud) javascript ×5
event-loop ×2
dom ×1
ecmascript-6 ×1
es6-promise ×1
generator ×1
html ×1
keydown ×1
keypress ×1
promise ×1
theory ×1