Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

55. 为什么我们需要回调 #72

Open
webVueBlog opened this issue Jun 9, 2022 · 0 comments
Open

55. 为什么我们需要回调 #72

webVueBlog opened this issue Jun 9, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Owner

需要回调,因为 javascript 是一种事件驱动语言。这意味着在侦听其他事件时,javascript 将继续执行而不是等待响应。让我们以第一个函数调用 API 调用(由 setTimeout 模拟)和记录消息的下一个函数为例。

function firstFunction() {
  // Simulate a code delay
  setTimeout(function () {
    console.log("First function called");
  }, 1000);
}
function secondFunction() {
  console.log("Second function called");
}
firstFunction();
secondFunction();

Output;
// Second function called
// First function called

从输出中可以看出,javascript 没有等待第一个函数的响应,而是执行了剩余的代码块。因此,回调用于确保某些代码在其他代码完成执行之前不会执行。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant