Pseudo-JavaScript code with inline comments:
// why not just new Fetch? hah
window.fetch = function(url, other) {
return new Fetch(url, other);
};
class Fetch {
cancel() {} // it absolutely should be cancelable!
_exitHelper() {
if (this._error) {
return this._errorPromise;
}
if (this._canceled) {
return this._foreverPendingPromise;
}
},
get headers() {
var exit = this._exitHelper();
if (exit) {
return exit;
}
return this._headersPromise;
},
constructor(url, other) {
this.response = new Promise(function() {
// resolve once network operation completely ended
});
}
}
var req = fetch('...');
req.headers.then(function(response) {
if (response.headers.get('Content-Type') === 'application/json') {
return response.json();
}
req.cancel(); // accept only json
});
var req = fetch('...');
req.headers.then(function(response) {
var reader = response.body.getReader();
// ...
});
var req = fetch('...');
req.response.then(function(response) {
// fully done here
});