Comments (2)
Aha I see, I was unsure what is called when. It's much clearer now. Thank you for the explanation!
from wretch.
Hi @edongashi,
Is defer in principle a "managed" (wretcher instead of fetchlike) version of middlewares? It feels much easier to configure the wretcher instead of dotting into the options of middlewares.
defer
and middlewares
share some similarities, but as you noted the key difference is that defer is synchronous and simpler.
A middleware
is more powerful in the sense that it has full power over the request and the response, whereas defer
can only configure the Wretcher
object but it is unable to inspect the response body (for instance).
Can defer be async?
Well it would be nice indeed, but I'm afraid it's impossible (and a crucial difference between middlewares and defer).
defer
is called just before performing the fetch request, when calling the http verb method in the chain:
wretch('...')
.get() // <- defer is called here implicitly
.json()
Here are the relevant lines in the source code:
private method(method, options = {}, body = null) {
/* … */
baseWretcher = baseWretcher.options({ ...options, method })
// If _deferredChain was be a chain of Promises,
// deferredWretcher would have to wait for their completion and be a Promise itself.
const deferredWretcher = baseWretcher._deferredChain.reduce((acc: Wretcher, curr) => curr(acc, acc._url, acc._options), baseWretcher)
return resolver(deferredWretcher)
}
The key point is that if defer
accepted async functions, it would mean that method()
would
return a Promise<...>
.
And this piece of code:
wretch('...')
.get()
.json()
Would turn into:
wretch('...')
.get()
.then(w => w.json())
Which defeats the purpose of this lib, since it breaks the fluid api.
from wretch.
Related Issues (20)
- Should no longer recommend form-data HOT 5
- Quick remedy for "Cannot find module 'wretch/middlewares'" ESM error when using Jest HOT 3
- Doc: Typescript examples? HOT 3
- How to Add a proxy? HOT 1
- Wretch with Zod HOT 2
- Introduce isWretchError property inside WretchError type HOT 1
- Leave request headers undefined HOT 2
- Introduce "graph" addon HOT 2
- Where is the full documentation? HOT 2
- Support for universal error catcher HOT 3
- CatherFallback type error HOT 3
- Using PerfsAddon HOT 2
- More resilient handling of `/` when concatenating urls HOT 6
- Timeout and catch it HOT 3
- Upload file with file;filename (multiple parameters) HOT 2
- How should 304 responses be handled? HOT 2
- Addon for upload progress HOT 5
- Error: Package subpath './middlewares' is not defined by "exports"
- Wrong condition in FormDataAddon and not converting FileList to `file[]` format HOT 1
- I get an error when I try to get an ArrayBuffer HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wretch.