如何将数据附加到 fetch() 请求并通过响应取回?
How to attach data to fetch() request and get it back with response?
是否可以使用 fetch() API 将任意仅客户端数据附加到请求,然后从响应中访问它?
我需要为每个请求附加一个序列号到一个特定的端点,我需要它通过相应的响应以某种方式可用(为了知道什么时候静静地放弃对 "superseded" 请求的响应), 但我既不需要也不想将此序列号发送到服务器并要求服务器 return 它。
只需将其存储在闭包中:
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();
});
}
是否可以使用 fetch() API 将任意仅客户端数据附加到请求,然后从响应中访问它?
我需要为每个请求附加一个序列号到一个特定的端点,我需要它通过相应的响应以某种方式可用(为了知道什么时候静静地放弃对 "superseded" 请求的响应), 但我既不需要也不想将此序列号发送到服务器并要求服务器 return 它。
只需将其存储在闭包中:
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();
});
}