Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!