title: Events firing twice inside axios interceptor
body: <p>I am trying to implement a interceptor with axios so everytime I receive a response from a request, it checks if it has the field &quot;msg&quot; and if it has, it should show the message to the user.</p>
  <p>To do this, I implemented a interceptor in axios, this interceptor should fire an event everytime it receives a response, then the <code>App.js</code> would be listening to this events to show the appropriate message.</p>
  <p>My problem is that it seems the event is being fired twice, at first I though it was a problem with <a href="https://www.npmjs.com/package/pubsub-js" rel="nofollow noreferrer">PubSub</a>, which is the library that I was using, so I decided to try another framework (<a href="https://www.npmjs.com/package/eventemitter3" rel="nofollow noreferrer">eventemitter3</a>) and the exact same thing happened, so I'm totally lost, other people who had similar issues found out that their requests werte actually being fired twice by different components, I'm almost sure that this isn't the problem here, look at the logs when I make a request:</p>
  <pre><code>interceptor &gt; Object { data: {…}, status: 200, statusText: &quot;OK&quot;, headers: {…}, config: {…}, request: XMLHttpRequest }
  intercepted by eventemitter3 &gt; Object { data: null, msg: &quot;Usuário não encontrado&quot;, success: true }
  intercepted by eventemitter3 &gt; Object { data: null, msg: &quot;Usuário não encontrado&quot;, success: true }
  intercepted by pubsub &gt; Object { data: null, msg: &quot;Usuário não encontrado&quot;, success: true }
  intercepted by pubsub &gt; Object { data: null, msg: &quot;Usuário não encontrado&quot;, success: true }
  </code></pre>
  <p>notice that there is one &quot;interceptor&quot; log, which means only one request was intercepted, then one event is fired for eventemitter3 and one event is fired for pubsub. The networks tab of the browser only shows one POST and one OPTIONS request. Here is my axiosInstance:</p>
  <pre><code>import Axios from 'axios';
  import PubSub from 'pubsub-js'
  import Emitter from './EventHandler';
  
  export const axios = Axios.create({
      baseURL: &quot;http://localhost/api&quot;
  })
  
  axios.defaults.headers.common['Authorization'] = localStorage.getItem('jwt') || &quot;&quot;;
  
  axios.interceptors.response.use(function (response) {
      console.log(&quot;interceptor&quot;,response)
      Emitter.emit('RESPONSE_INTERCEPTED', response.data);
      PubSub.publish('RESPONSE_INTERCEPTED', response.data);
      return response;
  }, function (error) {
      return Promise.reject(error);
  });
  </code></pre>
  <p>and here is App.js where I listen to the events:</p>
  <pre><code>export default function App() {
      const alert = useAlert()
  
      Emitter.on('RESPONSE_INTERCEPTED', (data) =&gt; {
          console.log(&quot;intercepted by eventemitter3&quot;, data)
          alert.show(data.msg)
      });
  
      var responseNotifier = function (msg, data) {
          if(data.msg){
              console.log(&quot;intercepted by pubsub&quot;, data)
          }
      };
  
      var token = PubSub.subscribe('RESPONSE_INTERCEPTED', responseNotifier);
  
      return (
          &lt;Router&gt;
              &lt;div&gt;
                  &lt;Switch&gt;
                      &lt;Route path=&quot;/sobre&quot;&gt;&lt;Sobre /&gt;&lt;/Route&gt;
                      &lt;Route path=&quot;/memorias&quot;&gt;&lt;Memorias /&gt;&lt;/Route&gt;
                      &lt;Route path=&quot;/login&quot;&gt;&lt;Login /&gt;&lt;/Route&gt;
                  &lt;/Switch&gt;
              &lt;/div&gt;
          &lt;/Router&gt;
      );
  }
  </code></pre>
  <p>just in case it matters, here is the <code>EventHandler</code> for eventemitter3:</p>
  <pre><code>import EventEmitter from 'eventemitter3';
  
  const eventEmitter = new EventEmitter();
  
  const Emitter = {
      on: (event, fn) =&gt; eventEmitter.on(event, fn),
      once: (event, fn) =&gt; eventEmitter.once(event, fn),
      off: (event, fn) =&gt; eventEmitter.off(event, fn),
      emit: (event, payload) =&gt; eventEmitter.emit(event, payload)
  }
  
  Object.freeze(Emitter);
  
  export default Emitter;
  </code></pre>
  <p>and the piece of code that makes the request:</p>
  <pre><code>    login(){
          axios.post('/login', {
              &quot;username&quot;: this.state.username,
              &quot;password&quot;: this.state.password
          }).then((resp)=&gt;{
              // console.log(resp)
          })
      }
  </code></pre>
  <p>I'm really clueless here, everything I found on SO/Google points to requests being fired twice, but the whole application is still in the begining, the code aboce is the only plaec where I fire a request, and the network tab confirms this, I'm fine using whatver event framework or even a completely different solution, I just need to show the message to the user, if someone can point me what I'm doing wrong here It would be of great help for me. Thank you all!</p>