3 ms·
Why? fetch returns a native promise, which are _much much much_ nicer to work with than XHR ever was or desired to be. Good riddance.
by tcd 8y ago
Why? fetch returns a native promise, which are _much much much_ nicer to work with than XHR ever was or desired to be.
Good riddance.
- arve0 8y agoMissing .abort() on safari is one reason. https://caniuse.com/#feat=abortcontroller https://caniuse.com/#feat=abortcontroller
- tracker1 8y agohttps://www.npmjs.com/package/ky https://www.npmjs.com/package/ky
- tyingq 8y agoStraightforward error handling both for actual http numbered errors, and for transport errors (site down/DNS/etc).
- aaaaaaaaaaab 8y agoFor one, fetch() doesn’t support progress reporting.
- voltagex_ 8y agoI'm not particularly familiar with fetch, but does this do what you want? https://github.com/SitePen/javascript-streams-blog-examples/blob/master/streaming-fetch/main.js https://github.com/SitePen/javascript-streams-blog-examples/...
- aaaaaaaaaaab 8y agoThis is a hack that works only for downloads via streams. It won’t work e.g. for file uploads (unless you read the file into memory first, which is highly undesirable).
- runarberg 8y agoIt does. The fetch response object exposes a `ReadableStream` through the `body` property, which you can loop through with a `for await ... of` loop. On each iteration, simply update your progress.
- tcd 8y agoSee [1] for a demo [1]:https://tejasq.github.io/basically-streams/examples/fetch/ https://tejasq.github.io/basically-streams/examples/fetch/
- aaaaaaaaaaab 8y agoYep, that’s a cute hack. Now what would be the equivalent of that for file upload? There’s no such API for fetch() because there’s no writeable body stream for requests. QED
- altmind 8y agojquery also returns a promise