Code Monkey home page Code Monkey logo

Comments (2)

edongashi avatar edongashi commented on July 22, 2024 1

Aha I see, I was unsure what is called when. It's much clearer now. Thank you for the explanation!

from wretch.

elbywan avatar elbywan commented on July 22, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.