Reputation: 3175
Is it possible with the fetch() API to attach arbitrary client-only data to the request and then access it from the response?
I need to attach a sequence number to each request to a particular endpoint and I need it to be somehow available through the corresponding response (in order to know when to silently drop a response to a "superseded" request), but I neither need nor want to send this seq number to the server and require that the server return it.
Upvotes: 0
Views: 312
Reputation: 665286
Just store it in the closure:
var seq = 0;
function makeRequest() {
var cur = ++seq;
return fetch(…).then(function(response) {
if (cur < seq)
throw new Error("has been superseded");
else
return response.json();
});
}
Upvotes: 1