Axios vs promise

What is the difference between axios and promises?

Which one should I use?

That’s kind of a weird question. Axios is a library for http-requests. Promises are a JS concept representing the result of an asynchronous operation. Axios is based on promises as probably almost all http libaries are.

3 Likes

I had tinkered with the axios library briefly, relearning promises, and async programing again in the node course. Had a hard time finding a straight answer on google. Thanks.

Create http module with axios… dont use fetch api. In large project, it means more flexibility and scalebility. You might create a module with fetch but there is really no point. This is not a questions of axios or promises. They all return promises…

Axios allows you to format all the requests sent to server, such as default url, headers, token, etc. And also intercept all requests based on types and do error handling or tasks in one place. With fetch, you will end up with hundreds of such api all over the places. It’d be hell if you needs to change something in the future, which you certainly will.

Another pro is that you can create instances of axios that are responsible for different routes, like user routes, message routes, etc if you need customization on the information handling.

A more advanced implementation will be: create a general HTTP class with axios in one module with all general implementation. Than customize different http modules based on your need (for different routes) with class inheritance. Mosh mentioned this in the Angular courses. Forget this if you are not familiar with js prototype inheritance concept. Not a lot of developers do it, certainly most small projects won’t need this.

1 Like