Sriraman
Sriraman

Reputation: 7947

Taking too much time to call promise's then function

I'm using fetch API to call query the server in my React Native Application. But, My application taking 50 seconds to call then function after receiving the response from the server. Am I doing any mistake or Is Promise working very slow?

fetch(url, {
      method: 'POST',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
      },
      body: bodyContent
    }.then((responseText) =>  {
        console.log(responseText);
        responseText.json().then(function(response){
             console.log(response);
        });
    });

response is printing in log 50 seconds after the responseText

UPDATE : Just now found that the responseText.json() promise is executing only after I tap on the screen again. This problem is weird.

Upvotes: 6

Views: 8753

Answers (5)

Sriraman
Sriraman

Reputation: 7947

Finally, I found the solution for this problem. It is because of the Chrome Debugging. If I stop chrome debugging, It is working fine. If Chrome debugger is running, I have to tap on the screen for the return value. So, Ignore this delay if you are running chrome debugger.

Upvotes: 6

Rick Li
Rick Li

Reputation: 11

I have encountered the same issue, the responseText comes back straight away in milliseconds, but when it convert to json using .json(), it takes a few seconds, the interesting thing is if I click the screen during the .json() parsing period, it get the json data back straight away

Upvotes: 0

Frederick Motte
Frederick Motte

Reputation: 1164

Since we narrowed it down to the json() call that takes too much time, it seems that this is a reported issue (https://github.com/facebook/react-native/issues/6418) that doesn't happen often and so far is not reproducible. It might have to do with structure or size of your json object.

Personally I use the code construct you use quite heavily in my react native apps and there is no performance penalty. However, my typical reponse is quite small and simple (e.g. a list of 10 objects with about 20 keys, no nesting etc.)

You could try the suggestion in the issue report I linked to and use responseText.text() and compare performance.

Upvotes: 3

Alex
Alex

Reputation: 4669

Try to open your webtool kit developper from your browser (commonly F12 key) and go into network tab.

You can see the time that your query take. If it takes long, it's either your network connection or the server which have a delayed response set.

If not, it's client side.

Upvotes: -1

promise little bit slower than callback. But not 50sec! I think you have problem with internet connection.

Upvotes: 1

Related Questions