0

I want to get time difference between a request & response. For example in below code what is time interval to complete the request ?

return fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
   **# How much time above fetch request took ?**
  return responseJson.movies;
})
priti narang
  • 258
  • 4
  • 18

2 Answers2

3

You can take current time using Date

var d = new Date();
var n = d.getTime();

before api call and after your response. and subtract both time.

You will get answer in miliseconds.

Jaydeep Galani
  • 4,842
  • 3
  • 27
  • 47
1

You Just need to set a variable with time before initiating the call and get the difference from current time inside the response method.

Lets say that this is your function

function getData()
{
 const start = new Date();
 return fetch('https://facebook.github.io/react-native/movies.json')
.then((response) => response.json())
.then((responseJson) => {
   const timeTaken= (new Date())-start;
  return responseJson.movies;
})

}

timeTaken will be the actual time taken for the request.

Guruparan Giritharan
  • 15,660
  • 4
  • 27
  • 50
  • Thanks for the reply. I know that, we can calculate time difference like you suggested. As we are dealing with javascript so its not accurate time calculation. I need some in-build solution like. response.status give us code 200 for success. Is there any method to get time like response.turnAroundTime etc.? – priti narang Feb 14 '19 at 07:04
  • If you can use something like axios you have more options https://stackoverflow.com/questions/49874594/how-to-get-response-times-from-axios – Guruparan Giritharan Feb 14 '19 at 07:45