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

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)