The definition from Wikipedia:
In computer science, a programming language is said to have first-class functions if it treats functions as first-class citizens. This means the language supports passing functions as arguments to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.
The definition is a bit confusing if we are reading it for the first time. However, the fact is that we used it even without knowing about it.
In line 9, we passed the function as an argument to the method addEventListener. The function is associated with the event “click” of the button. When the event is fired, the function will run.
Let’s be curious — Part 1
To understand the role of the feature, let’s think about languages where it isn’t available. Adding dynamic behaviors is something common in UI development, regardless of the programming language. What if we cannot pass the function as an argument? I am looking forward to your response in the comment section.
Again, we pass 2 functions as arguments of the
use method. The first function sets the token in the request’s header. The second function runs if there is an error (we don’t define it here for simplicity). When Axios processes a request, it will run all interceptors one by one to transform the user’s config to the full configuration. Then it will send the request to the server.
In line 23, the fulfilled and rejected functions we pass in the
usemethod are added to the requestInterceptorChain. We store functions in an array. Then Axios will run each of them. Inside the while loop, you can see functions are assigned to variables to be called.
Let’s be curious — Part 2
Sending HTTP requests is not limited to frontend development. When developing the backend part, we might need to send requests to an external server. Could you explain to our community how HTTP request configurations are processed in your preferred language?
Middleware functions are functions that have access to the request object (
req), the response object (
res), and the next middleware function in the application’s request-response cycle.
You can see the example of middleware below.
The middleware function is passed in the
use method. It, in turn, accepts another function
next as an argument. The
next function is called at the end to pass the control to the following middleware in the stack.
Express is popular and widely used for its simplicity. “An Express application is essentially a series of middleware function calls.” Despite the trivial looks, Express’s middleware can help us do all tasks of a web server: logging the request, compressing the response, setting cookies, preventing XSS attacks … just to mention a few.
Let’s be curious, again!
How HTTP requests are processed in other backend frameworks? Can you compare it to Express middleware? What are the advantages/disadvantages of each method? You see, there are a lot of questions to study!
Callback functions give us a powerful tool to deal with I/O bound applications. However, every good thing can go bad if it is abused. You can see the example below.
Multiple callback functions and if/else statements make the code hard to understand. It can become unmaintainable in the future if we add more logic. Because of this problem, newer features are introduced. Promises appeared to help us write a cleaner program. Async/await keyword allows us to write asynchronous code that looks like synchronous code.