- From: Andrea Giammarchi <notifications@github.com>
- Date: Mon, 30 Mar 2015 02:49:47 -0700
- To: whatwg/fetch <fetch@noreply.github.com>
- Message-ID: <whatwg/fetch/issues/27/87617663@github.com>
FWIW I've implemented a cancelable Promise playground which is already suitable for everything discussed in here: https://gist.github.com/WebReflection/a015c9c02ff2482d327e Here an example of how it works: ```js // will be resolved new Promise(function ($res, $rej, ifCanceled) { var internal = setTimeout($rej, 1000); ifCanceled(function () { clearTimeout(internal); }); }) // will be resolved without executing .then( function () { console.log('on time'); }, function () { console.log('error'); } ) .cancel() // will simply execute and resolve .then(function () { console.log('no time'); }); ``` The Promise is cancelable **only** if a function to describe how to cancel it is provided. This detail is hidden from the outer world. In this scenario/case it would virtually look like the following: ```js function fetch(url) { return new Promise(function (res, rej, ifCanceled) { var xhr = new XMLHttpRequest; xhr.open('GET', url, true); xhr.send(); // and the rest of the logic ifCanceled(function () { xhr.abort(); }); }); } // outside var page = fetch('index.html').then(function (text) { document.body.textContent = text; }); // at any time later on, if needed page.cancel().then(function () { console.log('nothing happened'); }); ``` All involved promises will be silently resolved. The developer has the ability to react and it does not need to expose any cancel-ability if not meant. The design is backward compatible with current Promise specification, without even requiring a `canceled` state. I'm not sure it's perfect, but it works already and it seems to be suitable for any sort of scenario. My 2 cents --- Reply to this email directly or view it on GitHub: https://github.com/whatwg/fetch/issues/27#issuecomment-87617663
Received on Monday, 30 March 2015 09:50:19 UTC