How to get response times from Axios

How to get response times from Axios



Can anyone suggest any ways to get response times from Axios? I've found axios-timing but I don't really like it (controversial, I know). I'm just wondering if anyone else has found some good ways to log response times.





What about Network tab on dev tools?
– Bk Santiago
Apr 17 at 9:36





It's as part of automated tests, so needs to be in code...
– rozza
Apr 18 at 7:02





2 Answers
2



You can use the interceptor concept of axios.



Request intercepor will set startTime


axios.interceptors.request.use(function (config)

config.metadata = startTime: new Date()
return config;
, function (error)
return Promise.reject(error);
);



Response interceptor will set endTime & calculate the duration


axios.interceptors.response.use(function (response)

response.config.metadata.endTime = new Date()
response.duration = response.config.metadata.endTime - response.config.metadata.startTime
return response;
, function (error)
error.config.metadata.endTime = new Date();
error.duration = error.config.metadata.endTime - error.config.metadata.startTime;
return Promise.reject(error);
);





Are we allowed to add random stuff to config?
– TA3
Aug 1 at 6:18





@TA3 yes, it works. As I did in the above case.
– Sagar Makwana
Aug 6 at 10:00



Its way long after but this is my simple workaround


function performSearch()
var start = Date.now();
var url='http://example.com';
var query='hello';
axios.post(url,'par1':query)
.then(function (res)
var millis = Date.now() - start;
$('.timer').html(""+Math.floor(millis/1000)+"s")
)
.catch(function (res)
console.log(res)
)





this is my workaround



Thanks for contributing an answer to Stack Overflow!



But avoid …



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid …



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Malaysia to Papua New Guinea by motorcycle?

.name

List of Bhojpuri films