Home > Web Front-end > JS Tutorial > Why does js callback?

Why does js callback?

little bottle
Release: 2019-05-31 13:20:31
Original
2355 people have browsed it

In Javascript, functions are first-class objects, which means that functions can be used like objects according to first-class management. Since functions are actually objects: they can be "stored" in variables, passed as function parameters, created within functions, and returned from functions. Because functions are first-class objects, we can use callback functions in Javascript. Let's learn about callbacks together.

Why does js callback?

Simply put: a callback refers to a function that is called after another function has completed execution

It’s a little more complicated To put it bluntly: In JavaScript, functions are also objects. Therefore, functions can be passed in functions as parameters and can also be returned by other functions. Such a function is called a higher-order function. The function passed in as a parameter is called a callback function.

Let’s talk about why we need callbacks?

There is a very important reason - JavaScript is an event-driven language. This means that JavaScript will not stop running while waiting for a response, but will continue executing while listening for other events.

Let’s look at a basic example:

function first(){
  console.log(1);
}
function second(){
  console.log(2);
}
first();
second();
Copy after login

As you would expect, the first function is executed first, and then the second function is executed - the console outputs the following:

// 1
// 2
Copy after login

But what if function first contains some code that cannot be executed immediately?

For example an API request where we have to send a request and then wait for a response? To simulate this situation, we will use setTimeout, which is a JavaScript function that calls a function after a period of time. We delay the function for 500 milliseconds to simulate an API request. The new code looks like this:

function first(){
// 模拟代码延迟
  setTimeout( function(){
console.log(1);
  }, 500 );
}
function second(){
  console.log(2);
}
first();
second();
Copy after login

It is not important to understand how setTimeout() works now. What is important is that you have seen that we have console.log( 1); Moved inside the 500 second delay function. So what happens when the function is called now?

first();
second();
// 2
// 1
Copy after login

Even though we call the first() function first, the output we record is after the second() function.

This is not a problem of JavaScript not executing functions in the order we want, but a problem of JavaScript not waiting for the response of first() before continuing down to second().

So why are you showing this?

Because you can't call functions one after another and expect them to execute in the correct order.

Callbacks are exactly the way to ensure that one piece of code is executed before another piece of code is executed.

The above is the detailed content of Why does js callback?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template