- From: Andrea Giammarchi <notifications@github.com>
- Date: Mon, 30 Mar 2015 16:15:07 -0700
- To: whatwg/fetch <fetch@noreply.github.com>
- Message-ID: <whatwg/fetch/issues/27/87873935@github.com>
@getify quite sad statement ... we just need to better understand each other I guess ... so here the scenario I am describing against `async` and `await`:
```js
async function fetchLike(url) {
try {
var response = await ajax(url);
return response.text;
}
catch (err) {
// ..
}
}
var asyncPromise = fetchLike("http://some.url.1");
// after this ...
// var response = await ajax(url);
// we are virtually here^
asyncPromise.cancel({});
// ajax(url).cancel({});
// the engine is implicitly doing ^^^^^^^^^^^^
// since canceling, the engine is also
// executing this instead of the original function
async function fetchLike(url) {
try {
var response; return ajax(url).cancel({});
// see this ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// that's what a cancel-able await does
return response.text;
}
catch (err) {
// ..
}
}
// outside, in a better world ...
asyncPromise.then(function (value) {
// empty result, nothing to do
return value; // keep going, if needed
});
```
That works to me, it's like the return inside a generator, not so different at all.
Do you need to know what happens inside `ajax(url)` when `ajax(url).cancel({})` happens? **NO**, because that's the whole point. If you provide a cancelable Promise, you are the ony one that can resolve or reject it internally, *so you are the only one able to cancel*.
You provide such functionality? Others can consume it. It's exactly all the same with controllers, except you pass around directly a cancel-albe Promise instead of the Promise plus its controller.
Do you want to hide this ability?
```js
async function fetchLike(url) {
try {
var response = await Promise.resolve(ajax(url));
// check this out ^^^^^^^^^^^^^^^^^^^^^^^^^^^
return response.text;
}
catch (err) {
// ..
}
}
```
You know what's the result? That if you try to cancel `fetchLike` now, since `Promise.resolve` does **not** return a cancel-able Promise, it will throw an error, 'cause you are not allowed.
So this pattern covers every scenario discussed in here, or am I missing anything at all?
If the question is again: how do you cancel that from the outer world? ... the answer would be again: You don't! You are not responsible for cancel-ability because as the Promise creator provides `resolve` and `reject`, the Promise creator is the ony one that can provide a `cancel` too without exposing its mechanism.
I rather prefer some sort of agreement instead of abandonment. This is the future of the asynchronous Web, this must be done bloody right! (and I feel it's too late already)
---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/27#issuecomment-87873935
Received on Monday, 30 March 2015 23:15:30 UTC