chinesedfan/You-Dont-Know-Axios

Why do you need interceptors in design?

Closed this issue · 7 comments

You may handle data before api call and handle data after api call, right

I am not the author of axios. But this issue is a question with the answer.

Nope, there is no answer. I don't need interceptors, do you?

Maybe, without interceptors, you can also achieve your features. But it doesn't mean they are useless. Developers can divide logics into several independent parts and organize codes more clearly.

If my words didn't solve your problem, koa, express, redux, and lots of other libraries leverage this kind of middleware mechanism, you can also open issues to those repositories.

No, I don't state they are useless, only "why" my motivation. I've seen you closing a lot of issues at the axios repo, so decided to ask you.

Again, Im not demanding to solve my stuff, only discussing things. Didn't know much about others, so they do the same.. okay.

Because this question was too basic and too wide to discuss, and you didn't introduce any backgrounds, I felt confused at first.

There is no exact reason about why design interceptors. I have tried to explain from the positive side. And you can also think from the other side. Imaging without interceptors, what changes in our codes.

An example not sure applicable is why design clothes. Someone may like to be naked, and be fine with leaves. But clothes make things easier to be replaced and maintained.

Okay, probably they are good in case of Promise syntax, you specify interceptor funcs and then asseble it somewhere.. in case of async/await syntax, they have less worth. So,

Benefits

  • simplifies Promises chain
  • reduces code by incapsulation

Contras

  • may interfere with async/await
  • removes some logic out of a call context

if you agree :|

Not really. Interceptors can also work with async/await, as long as you consider async functions as common functions which return a Promise.