- From: Arthur Stolyar <notifications@github.com>
- Date: Tue, 24 Mar 2015 04:46:05 -0700
- To: slightlyoff/ServiceWorker <ServiceWorker@noreply.github.com>
- Message-ID: <slightlyoff/ServiceWorker/issues/592/85461838@github.com>
I have one more proposal for canceling ```fetch```. Let's image what ```fetch``` should not return ```Promise``` (or any subclasses of it if it's hard for now). but some ```FetchObject``` with similar signature as ```Promise```:
```javascript
FetchObject = {
// promise-like
then: function() {},
catch: function() {},
// cancel() method not related to promise at all
cancel: function() {}
};
```
```then()``` and ```catch()``` will pretend as FetchObject is promise (like third-party promises), but will return native ```Promise```. In the meantime, ```cancel()``` will be related to ```fetch``` itself and its execution e.g. aborting request and preventing promise to be settled (or rejecting it, I cannot remember which action you already choose for it) if it was not already, and closing the stream (sorry I am not familiar with steams terminology here) if promise was fullfilled/stream was used.
In general, usage would look like this:
```javascript
var reqSomething = fetch('...');
var asyncDoSomethig = ...;
var onceDone = Promise.all([
reqSomething.then(function handleStream() { ... });,
asyncRenderSomething
]);
onceDone.then(function(args) {
// apply some stuff
});
onEscPressed(function() {
reqSomething.cancel();
});
```
So here ```onceDoen.then``` will never called if esc would be pressed before fetch is completely handled.
Few other things:
As someone pointed out, canceling is not always required it will be used once needed, for example this will not work, because ```reqSomething``` will not be ```FetchObject```, but regular ```Promise```:
```javascript
var reqSomething = fetch('...').reqSomething.then(function handleStream() { ... });
reqSomething.cancel();
```
So once someone needed to cancel fetch, then they may store origin ```FetchObject``` to do so. And, of course, it would be to have ```cancel``` event for ```FetchObject```, so it probably should be implement ```EventTarget``` too.
This was seems not ideal, but other ways are not better. Thanks.
---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/592#issuecomment-85461838
Received on Tuesday, 24 March 2015 11:46:33 UTC