Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

105
Vistas
Complex method chaining

For context, I'm thinking about this type of method chaining, in which the final method called returns a promise. My question is different from this one, however.

I'm truly stumped with this, so bear with me on my lack of reproducible examples. I'm not even too sure where to begin with this. It's also not a homework question.

Consider the function foo on the class Bar, which takes in a few parameters. foo is asynchronous and returns a promise. baz is totally optional, and can be chained onto foo (foo(param).baz(someOtherParam)), which will affect the way foo is eventually run. What's important to note here is that foo will return without the chained baz being called, but if baz IS called, it will be the one who returns the promise (not foo).

My explanation is probably confusing, so here's a visual example:

// The promise is returned by the .where() 
const data = await User.where({ age: { $gte: 21 } })

// The .gte() function affects the execution. The promise is returned from .gte()
const data = await User.where('age').gte(21)

// .gte() doesn't return the promise anymore. The second chained .where() does.
// But also we can just continue chaining onto that pretty much endlessly if we want to.
const data = await User.where('age').gte(21).where('name', /^b/i)

I understand how to method chain with synchronous functions just fine - just return an object with some methods on it and you're pretty much good to go. What has me stumped is this implementation, which you can see a lot in libraries.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda