Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

106
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda