有人可以知道为什么备回函数以后执行吗?
app.post("/login",function(req,res){
var name = req.body.username;
var pass = req.body.password;
Account.countDocuments({username:name, password:pass}, function(err , count){
if(count > 0){
Account.find({username:name, password:pass}, function(err,data){
prof = data[0].profile;
console.log(prof + "Executes Second"); // here it executes second
})
console.log(prof+" Executes First"); // here it executes first
res.render("index",{
url : prof,
});
}
else{
console.log("no account");
res.render("login",{
error : "You dont have a account !"
});
}
})
})
/我只是将图像URL传递到EJS文件,但我想知道为什么回电。稍后执行/
app.post("/login",function(req,res){
var name = req.body.username;
var pass = req.body.password;
Account.countDocuments({username:name, password:pass}, function(err , count){
if(count > 0){
Account.find({username:name, password:pass}, function(err,data){
prof = data[0].profile;
console.log(prof + "Executes Second"); // here it executes second
})
console.log(prof+" Executes First"); // here it executes first
res.render("index",{
url : prof,
});
}
else{
console.log("no account");
res.render("login",{
error : "You dont have a account !"
});
}
})
})
/I just passing the image url to ejs file but I want to know why the call back func. executing later/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为事件循环和JavaScript的堆栈调用是如何构建的。我发现这文章非常有见地。我希望你们都一样。
但是要用很少的话来解释它:
“
假设我们有
堆栈定义了函数和表达式执行的顺序。当触发回调函数时,它将在堆栈之外执行,其结果将进入一个队列,仅一旦堆栈为空,才会从中进行。因此,即使这是一个非常简单的操作,一旦调用了 chaterfst ,它将在堆栈之外执行,然后结果将停留在队列中,因为与此同时,堆栈已经存在< strong>无回调函数称为Second ,因此您将在 CAND FARNST 的结果之前看到其结果”
It's because of how the event loop and stack call of JavaScript are built. I find this article very insightful. And I hope it will be the same for y'all.
but to explain it in very few words I'd say:
"
Let's suppose we have
The stack defines the order of execution of the functions and expressions. When a callback function is triggered it is executed outside of the stack and its result will then go to a queue from which it will be taken only once the stack is empty. Therefore even if it is a very simple operation, once calledFirst is called, it will be executed outside of the stack and then the result will stay in the queue because in the meanwhile the stack will already be present calledSecond with no callback function so you will see its result before the result of calledFirst"