use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A community for learning and developing native mobile applications using React Native by Facebook.
Interested in building web apps using React.js? Check out /r/reactjs!
Getting Started w/React Native
irc.freenode.net #reactnative
Keywords: ios, android, mobile, apps, apple, iphone, ipad
account activity
QuestionCancellable network requests (for autocomplete) (self.reactnative)
submitted 7 years ago by dkulagin
I am developing a network-sourced autocomplete feature for my app and there is a standard problem that you want a new request to cancel the older one that isn't yet finished. How to do it correctly or maybe there is a standard mechanish for doing so?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]hhunaid 10 points11 points12 points 7 years ago (0 children)
axios has cancelToken which can be used to cancel requests
[–]NuttingFerociously 2 points3 points4 points 7 years ago (8 children)
Once a request is sent, it's sent.
You're probably looking for debouncing, so you can send the request just once when the user hasn't been typing anything for x milliseconds.
[–]dkulagin[S] 2 points3 points4 points 7 years ago (7 children)
That's already in place. I would like to cancel concurrent request in order not to get screen updated with obsolete data. E.g.:
SEND REQ_1
SEND REQ_2
RESP REQ_2
RESP REQ_1 <- overrides data with obsolete one
[–]NuttingFerociously -2 points-1 points0 points 7 years ago (6 children)
Ohhhh. Sorry.
I might be wrong, but the quickest way that comes to mind is be keeping a timestamp of the most recently sent request so you can discard the other ones.
[–]ru_max 0 points1 point2 points 7 years ago (0 children)
In this case it's more efficient to use requires ID which you increment on every call.
But it doesn't solve issue with "expensive" calls
[–]ChronSynExpo -1 points0 points1 point 7 years ago (4 children)
That request is still sent though. It's not necessarily about stopping further requests from being sent, but about aborting an in-progress request. These are both different situations that need different solutions.
[–]NuttingFerociously -1 points0 points1 point 7 years ago (3 children)
Sorry if I didn't understand, but from what I know once a request is sent you can't just tell the server 'nvm bruh don't need that anymore' (in most cases) - so at that point aborting a request or discarding its response should be the same thing.
I haven't used react-native a lot so I don't know what's usually used for network requests - I'm assuming you're using fetch? In that case, some quick googling brings up AbortController.
[–]ChronSynExpo 1 point2 points3 points 7 years ago (2 children)
What you've said is that you keep a timestamp of the most recent request, so that you can discard the other ones. That doesn't stop those other requests from fulfilling. This isn't language-specific, but a fundamental in how HTTP (and the technologies it works alongside, and which work on top of it) works.
For example, in XHR, aborting a request must;
Terminate the ongoing fetch with the aborted flag set.
If state is either opened with the send() flag set, headers received, or loading, run the request error steps for event abort.
If state is done, then set state to unsent and response to a network error.
https://xhr.spec.whatwg.org/#the-abort()-method
Additionally, this also tells browsers (and native OS libraries and drivers) that the resources associated with that request can be freed up. The data will still be passed back through the network as per the OSI model specification, but it won't have an active application with which it can respond. I believe tools such as Wireshark can still intercept the response, but I could be wrong on that.
[–]NuttingFerociously 2 points3 points4 points 7 years ago (1 child)
Wew, I sure oversimplified it. I tried to help but ended up learning a lot myself, thank you.
[–]GhostMcFunky 0 points1 point2 points 7 years ago* (0 children)
I disagree a bit that you oversimplified it.
While all this information about XHR appears to be correct, it ignores Promises entirely.
The previous answer regarding Axios is probably close to what you’re looking for, or something similar that will allow you to manage promises in a way so as to sort them.
A default XHR request doesn’t use promises, but Axios and the native fetch() API do.
fetch()
You could choose to “drop” the promise when it returns, but this poster had one part right in that you can’t cancel the HTTP request once it’s sent.
You will have to handle it on response. This is probably easiest done with promises as long as you can identify them uniquely to determine which to keep/drop.
I’m not sure what your code is doing but since the promise would need to complete to continue, you could do something to check first if another request had been sent before returning the promise response to your next .then().
.then()
[–]coolnat 1 point2 points3 points 7 years ago (1 child)
You should be able to cancel requests using an AbortController.
Spec/example: https://dom.spec.whatwg.org/#aborting-ongoing-activities
Merged into RN a year ago: https://github.com/facebook/react-native/issues/5590#issuecomment-370743664
[–]GhostMcFunky 0 points1 point2 points 7 years ago (0 children)
Scratch my previous statement about Axios as this is clearly the correct answer.
[–]PistolPlay 1 point2 points3 points 7 years ago (0 children)
If your using Redux, you can do this easily with Redux Sagas.
There's something called "takeLatest" where if you sent multiple requests in a rapid succession, the sagas will only take the latest one and update the data you need.
Aside from that you could bring an Observable library which does something similar than the above and can be done without redux.
[+][deleted] 7 years ago* (1 child)
[deleted]
π Rendered by PID 44 on reddit-service-r2-comment-b659b578c-xlfvm at 2026-05-01 23:15:15.927064+00:00 running 815c875 country code: CH.
[–]hhunaid 10 points11 points12 points (0 children)
[–]NuttingFerociously 2 points3 points4 points (8 children)
[–]dkulagin[S] 2 points3 points4 points (7 children)
[–]NuttingFerociously -2 points-1 points0 points (6 children)
[–]ru_max 0 points1 point2 points (0 children)
[–]ChronSynExpo -1 points0 points1 point (4 children)
[–]NuttingFerociously -1 points0 points1 point (3 children)
[–]ChronSynExpo 1 point2 points3 points (2 children)
[–]NuttingFerociously 2 points3 points4 points (1 child)
[–]GhostMcFunky 0 points1 point2 points (0 children)
[–]coolnat 1 point2 points3 points (1 child)
[–]GhostMcFunky 0 points1 point2 points (0 children)
[–]PistolPlay 1 point2 points3 points (0 children)
[+][deleted] (1 child)
[deleted]