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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With