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

111
Views
Javascript conditional method chaining

How can I simplify this code to use conditional method chaining for useBuffer() method. Please suggest.

  const getApi = async (headers, userContext, httpRequest, url, isBlob) => {
  if (isBlob) {
    const result = await httpRequest
      .get(url)
      .withCredentials()
      .set(headers)
      .send({ userContext })
      .useBuffer()
      .then((res) => [res.body, res.headers]);
    return result;
  }
  else {
    const result = await httpRequest
      .get(url)
      .withCredentials()
      .set(headers)
      .send({ userContext })
      .then((res) => res);
    return result;
  }
};

to something like this:

const getApi = async (headers, userContext, httpRequest, url, isBlob) => {
    const result = await httpRequest
      .get(url)
      .withCredentials()
      .set(headers)
      .send({ userContext })
      .isBlob && useBuffer()
      .then((res) => isBlob ? [res.body, res.headers] : res);
    return result;
};
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

How about saving the earlier chain in a variable?

const getApi = async (headers, userContext, httpRequest, url, isBlob) => {
    const sent = await httpRequest
        .get(url)
        .withCredentials()
        .set(headers)
        .send({ userContext })
    return !isBlob
        ? sent
        : sent
            .useBuffer()
            .then((res) => [res.body, res.headers]);
};

The .then((res) => res); is completely superfluous and so can be removed.

about 4 years ago · Juan Pablo Isaza Report

0

.then((res) => res) is redundant and boolean parameters that control structure are questionable design, so how about:

const getApi = (headers, userContext, httpRequest, url) =>
  httpRequest
    .get(url)
    .withCredentials()
    .set(headers)
    .send({ userContext });

const getApiBlob = (headers, userContext, httpRequest, url) =>
  getApi(headers, userContext, httpRequest, url)
    .useBuffer()
    .then((res) => [res.body, res.headers]);
about 4 years ago · Juan Pablo Isaza Report
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!