question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

response.text() and response.json() get "stuck" sometimes

See original GitHub issue

My code more or less does the following, on React Native 0.45:

async function request() {
    const result = await fetch(url);
    const data = await result.text();
    console.log(data);
    return data;
}

When running this in Chrome Debugger on the iOS Simulator, the result will get “stuck” sometimes. By that, I mean the first time I call it, it works fine. But the second time I run a request, the data will not be printed out until something else happens to jiggle the event loop and cause it to return.

If I disable the chrome debugger, things work.

I assume this is related to what people have been complaining about on the prematurely-closed bug https://github.com/facebook/react-native/issues/6679 .

=================

What follows is my attempt at debugging. I won’t promise anything, as I’m not a promise expert, but maybe it helps narrow things down?

The http response data is shuffled into the response.blob. When text() or json() is called, it calls readBlobAsText to read the blob, and I do see the string successfully loaded and passed around within the promise/setimmediate/core.js code. Unfortunately, this string doesn’t always make it back to the caller for some reason.

(This might be a red herring, but I see it call handleResolved() and the equivalent of setImmediate((self, deferred) => tryCallOne(deferred.onFulfilled, self._65 /* body payload */)), but the anonymous function isn’t actually called immediately…)

And interesting to note, if I edit fetch.js to disable support.blob, it also works.

This makes me suspect it has something to do with the text() function. In most cases, it returns Promise.reject or Promise.resolve, but in the case of using blobs, it returns a newly-generated Promise (from the fileReaderReady function). My guess is that this probably triggers another level of promise-indirection, that somehow causes it to stall before the event-loop notices it and triggers it to complete.

I’m still quite a newb as to the JSTimers* code and guts of the promise code, so this is where the trail goes cold for me. 😦

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
mikelambertcommented, Jun 16, 2017

And the modified ES7 form of the above code that also works for me (thanks!):

    setTimeout(() => null, 0);
    const json = await result.json();

Just add the setTimeout before you call json() or text() or anything else that operates on response.blob.

As far as the underlying bug, definitely feels like the eventloop “falls asleep” when processing response.blob and readBlobAsText, instead of recognizing there are still a Promise on the eventloop to process and return data back to the callers.

5reactions
huedscommented, Jun 15, 2017

Just upgraded and ran into this bug. Using @sebandres’ quick hack to force it to complete.

fetch(url).then(response => {
  setTimeout(() => null, 0);  // workaround for issue-6679
  return response.json();
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

JavaScript fetch - Failed to execute 'json' on 'Response'
The clone() method of the Response interface creates a clone of a response object, identical in every way, but stored in a different...
Read more >
API Routes: Response Helpers - Next.js
API Routes include a set of Express.js-like methods for the response to help you creating new API endpoints. Learn how it works here....
Read more >
Response.text() - Web APIs - MDN Web Docs
It returns a promise that resolves with a String . The response is always decoded using UTF-8.
Read more >
Twitter API Response Codes & Error Support
Get Twitter API response codes and error support through Twitter Developer here.
Read more >
Developer Interface — Requests 2.28.1 documentation
json – (optional) A JSON serializable Python object to send in the body of the ... Session() as s: ... s.get('https://httpbin.org/get') <Response [200]>....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found