4 ms·
Unlike an XMLHttpRequest which can be aborted, the new fetch() API does not provide a way for you to abort the request. This is because there is no standard way
by idbehold 10y ago
Unlike an XMLHttpRequest which can be aborted, the new fetch() API does not provide a way for you to abort the request. This is because there is no standard way to cancel a Promise which is what fetch() returns.
- couchand 10y agoSo.... we introduced Promises so that we could introduce fetch() that returns a Promise, only to discover that it meant we couldn't get all the other useful behavior of XMLHttpRequest. So while a proposal to add cancelable Promises would fix the inability to abort, we'd still need yet more additions to completely get back to functionality we had previously.
- idbehold 10y agoThat wasn't the reason promises were introduced. An XMLHttpRequest can be synchronous which is Bad™ and that is something explicitly not allowed by the Promise/A+ spec which means it will never be something that fetch() could do. The advantage of promises is that they can be `await`ed in the context of an `async` function which takes a lot of the cognitive load off of the developer.
- Klathmon 10y agoFurthermore the `await` syntax is one of the reasons why cancelable promises is contentious. There's no easy way to "cancel" an awaited promise without basically resorting to the .then() syntax.
- Sacho 10y agoCan't you use bluebird promises with fetch to add cancellation? I haven't tried it. I suppose you may want abort semantics but those appear to be impossible to implement in a generic way.