Skip to content

Commit

Permalink
fix: return the same value when resolving request multiple times
Browse files Browse the repository at this point in the history
Before this commit, when calling fetchr on the client using the
promise API and resolving it multiple times would lead to undefined
values.

We now make sure to always return the fetch promise in
httpRequest. But doing so, broke the abort behavior since we would not
return an FetchrHttpRequest to the user from fetchr methods. To fix
that, we now save an instance of FetchrHttpRequest in the Request
class and added an abort method to the later one to call
FetchrHttpRequest.abort method.
  • Loading branch information
pablopalacios committed Aug 15, 2024
1 parent fec3e1b commit 34b6d4b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
76 changes: 42 additions & 34 deletions libs/fetcher.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function Request(operation, resource, options) {
this._body = null;
this._clientConfig = {};
this._startTime = 0;
this._request = null;
}

/**
Expand Down Expand Up @@ -104,22 +105,39 @@ Request.prototype._captureMetaAndStats = function (err, result) {
}
};

Request.prototype._send = function () {
if (this._request) {
return this._request;
}

this._startTime = Date.now();
this._request = httpRequest(normalizeOptions(this));
var captureMetaAndStats = this._captureMetaAndStats.bind(this);

this._request.then(
function (result) {
captureMetaAndStats(null, result);
return result;
},
function (err) {
captureMetaAndStats(err);
throw err;
},
);

return this._request;
};

Request.prototype.then = function (resolve, reject) {
return this.end(function (err, data, meta) {
if (err) {
reject(err);
} else {
resolve({ data, meta });
}
});
return this._send().then(resolve, reject);
};

Request.prototype.catch = function (reject) {
return this.end(function (err) {
if (err) {
reject(err);
}
});
return this._send().catch(reject);
};

Request.prototype.abort = function () {
return this._request.abort();
};

/**
Expand All @@ -136,30 +154,20 @@ Request.prototype.end = function (callback) {
);
}

var self = this;
self._startTime = Date.now();
this._send();

var onResponse = function (err, result) {
self._captureMetaAndStats(err, result);
if (callback) {
setTimeout(function () {
callback(err, result && result.data, result && result.meta);
});
} else if (err) {
throw err;
} else {
return result;
}
};
if (callback) {
this._request.then(
function (result) {
callback(null, result && result.data, result && result.meta);
},
function (err) {
callback(err);
},
);
}

return httpRequest(normalizeOptions(self)).then(
function (result) {
return onResponse(null, result);
},
function (err) {
return onResponse(err);
},
);
return this._request;
};

/**
Expand Down
6 changes: 2 additions & 4 deletions libs/util/httpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,11 @@ FetchrHttpRequest.prototype.abort = function () {
};

FetchrHttpRequest.prototype.then = function (resolve, reject) {
this._request = this._request.then(resolve, reject);
return this;
return this._request.then(resolve, reject);
};

FetchrHttpRequest.prototype.catch = function (reject) {
this._request = this._request.catch(reject);
return this;
return this._request.catch(reject);
};

function httpRequest(options) {
Expand Down

0 comments on commit 34b6d4b

Please sign in to comment.