Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 575
Sriraman Avatar asked Feb 08 '23 07:02

Sriraman


2 Answers

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.

like image 119
Sriraman Avatar answered Feb 10 '23 22:02

Sriraman


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.

like image 37
Frederick Motte Avatar answered Feb 10 '23 23:02

Frederick Motte