DEV Community

Saravanan s
Saravanan s

Posted on

Closure, Fetch, and Axios in JavaScript

what is axios
1 It is open source library for making the HTTP request to the server

2 It Handling the error better than the fetch()

3 It automatically transforming the request in to json formate.

Example

axios.get("https://fakestoreapi.com/products"")
      .then(res => {
        console.log(res);
      })
      .catch(err => {
        console.error(err);
      });
Enter fullscreen mode Exit fullscreen mode

How to install the axios
In linux

CODE
*

        npm install axios
Enter fullscreen mode Exit fullscreen mode

Fetch() function in ja
The fetch() API in JavaScript provides a modern and promise-based way to make network requests, such as fetching data from a server or submitting form data. It offers a more flexible and powerful alternative to the older XMLHttpRequest (XHR) method

Example

<script>
fetch("https://fakestoreapi.com/products")
.then((res)=> res.json())
.then(jsonresponse)=>console.log(jsonresponse)
.catch((rej)=>rej.json())
</script>
Enter fullscreen mode Exit fullscreen mode

Fetch function arguments
The fetch() function takes one mandatory argument, the URL of the resource to fetch, and returns a Promise that resolves to a Response object.
JavaScript

Closure in js
A closure is the combination of a function bundled together (enclosed) with references to its surrounding state (the lexical environment). In other words, a closure gives a function access to its outer scope. In JavaScript, closures are created every time a function is created, at function creation

Example

<script>
function account(amount){
let balance = amount;
function deposite(amt){
balance=balance+amt;
console.log(balance);}

return deposite;
}
const deposit1 = account(1000);
deposit1.deposite(500);
</script>
Enter fullscreen mode Exit fullscreen mode

And I also knowledge about the JSON function in js JSON-JavaScript Object Notation

What is JSON
JSON, or JavaScript Object Notation, is a lightweight data-interchange format that is widely used in web applications for storing and transmitting data between a server and a client

Top comments (0)