Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit 856be43

Browse files
authored
Merge pull request #187 from jamesplease/race-condition-fix
Race condition fix
2 parents a1fe60b + 3a5c00a commit 856be43

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
### v3.1.1 (2018/7/10)
4+
5+
**Bug Fixes**
6+
7+
- A race condition has been fixed.
8+
39
### v3.1.0 (2018/7/10)
410

511
**New Features**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-request",
3-
"version": "3.1.0",
3+
"version": "3.1.1",
44
"description": "Declarative HTTP requests with React.",
55
"main": "lib/index.js",
66
"module": "es/index.js",

src/fetch.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ export class Fetch extends React.Component {
136136
// When a request is already in flight, and a new one is
137137
// configured, then we need to "cancel" the previous one.
138138
cancelExistingRequest = reason => {
139-
if (this.state.fetching && !this.hasHandledNetworkResponse) {
139+
if (this.state.fetching && this._currentRequestKey !== null) {
140140
const abortError = new Error(reason);
141141
// This is an effort to mimic the error that is created when a
142142
// fetch is actually aborted using the AbortController API.
@@ -214,6 +214,8 @@ export class Fetch extends React.Component {
214214
const requestKey = this.getRequestKey(options);
215215
const requestOptions = Object.assign({}, this.props, options);
216216

217+
this._currentRequestKey = requestKey;
218+
217219
const {
218220
url,
219221
body,
@@ -261,8 +263,6 @@ export class Fetch extends React.Component {
261263
// cancel the in-flight request.
262264
this.responseReceivedInfo = responseReceivedInfo;
263265

264-
this.hasHandledNetworkResponse = false;
265-
266266
const fetchPolicy = this.getFetchPolicy();
267267

268268
let cachedResponse;
@@ -315,7 +315,7 @@ export class Fetch extends React.Component {
315315
responseCache[requestKey] = res;
316316
}
317317

318-
if (!this.hasHandledNetworkResponse) {
318+
if (this._currentRequestKey === requestKey) {
319319
this.onResponseReceived({
320320
...responseReceivedInfo,
321321
response: res,
@@ -327,7 +327,7 @@ export class Fetch extends React.Component {
327327
return res;
328328
},
329329
error => {
330-
if (!this.hasHandledNetworkResponse) {
330+
if (this._currentRequestKey === requestKey) {
331331
this.onResponseReceived({
332332
...responseReceivedInfo,
333333
error,
@@ -358,7 +358,7 @@ export class Fetch extends React.Component {
358358
this.responseReceivedInfo = null;
359359

360360
if (!stillFetching) {
361-
this.hasHandledNetworkResponse = true;
361+
this._currentRequestKey = null;
362362
}
363363

364364
let data;

0 commit comments

Comments
 (0)