Skip to content

Instantly share code, notes, and snippets.

@nemanja947
Last active October 7, 2023 01:44
Show Gist options
  • Save nemanja947/a71018e659f7d5255ea43c304f8ee108 to your computer and use it in GitHub Desktop.
Save nemanja947/a71018e659f7d5255ea43c304f8ee108 to your computer and use it in GitHub Desktop.
NProgress implementation with Axios
// Add a request interceptor
axios.interceptors.request.use(function (config) {
// Do something before request is sent
NProgress.start();
return config;
}, function (error) {
// Do something with request error
console.error(error)
return Promise.reject(error);
});
// Add a response interceptor
axios.interceptors.response.use(function (response) {
// Do something with response data
NProgress.done();
return response;
}, function (error) {
// Do something with response error
console.error(error)
return Promise.reject(error);
});
@kofiasare
Copy link

Straight forward
thanks

@rahulspace
Copy link

Nice one. But i did progress done on error too.

// Add a request interceptor
axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  NProgress.start();
  return config;
}, function (error) {
  // Do something with request error
  NProgress.done();
  console.error(error)
  return Promise.reject(error);
});

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Do something with response data
  NProgress.done();
  return response;
}, function (error) {
  // Do something with response error
  NProgress.done();
  console.error(error)
  return Promise.reject(error);
});

@Kaishiyoku
Copy link

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment