Understand “Promise” (1/3): Axios

Step 0: I assume you have already read some documents and confusing now. Also, I assume you have already known how to use “axios”.



Step 1: If you don’t understand “Promise”, start from “axios”.



Step 2: But before that, you need to know “Promise” is a class.



Step 3: Then “axios” is based on “Promise”, what it means, it means each time when you call no matter “get” or “post” method of “axios”, it will return a “Promise” instance, look at the code below:

1
2
3
// This is a simple ajax request using the simplest axios call, it doesn't have a "then" or "catch" now.
// The code below will return a "Promise" instance.
axios.get("/api/items");


Step 4: Since the code above return us a “Promise” instance, now we can call “then” and “catch” of that instance, because it’s a “Promise” instance.😓

1
2
3
4
5
6
7
axios.get("/api/items")
.then((res)=>{
    console.log(res);
})
.catch((err)=>{
    console.log(err);
});


Next: Understand “Promise” (2/3): Promise Instance

Leave a Reply

Your email address will not be published. Required fields are marked *