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

264
Views
How to avoid then() nesting with js promises?

I'm writing an endpoint which modifies caption in a firebase entry; then collects all Typesense entries related to the firebase entry and fixes them too.

My code works exactly as I'd want it to. But I ended up going one level deeper with the .then() nesting than I would have liked to, resulting in 2 .catch() statements instead of one.

It somehow feels this is formally wrong and I'd love to fix it, but I don't know how to get myself "out" of it.

db.collection('posts').doc(post.id)
  .update({
    caption: post.caption
  })
  .then((data) => {
    // tsense collect all with fireId
    let searchParameters = {
      q: '*',
      filter_by: `fireId:=${post.id}`
    }
    TsenseClient.collections(Collection).documents().search(searchParameters)
      .then((data) => {
        console.log(data);
        return data
      })
      .then((tsenses) => {
        // tsense update all with fireId
        let tsenseUpdates = []
        tsenses.hits.forEach((hit) => {
          let doc = {
            "caption": post.caption
          }
          tsenseUpdates.push(TsenseClient.collections(Collection).documents(hit.id).search(searchParameters))
        })
        return Promise.allSettled(tsenseUpdates)
      })
      .then((tsenseUpdates) => {
        response.send('Update done.')
      })
      .catch((err) => {
        console.log(err);
      })
  })
  .catch((err) => {
    console.log(err);
  })
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Please focus on part

.then((data) => {
  // tsense collect all with fireId
  let searchParameters = {
    q: '*',
    filter_by: `fireId:=${post.id}`
  }
  TsenseClient.collections(Collection).documents().search(searchParameters)
  .then((data) => {
    console.log(data);
    return data
  }) 

and replace with:

.then((data) => {
  // tsense collect all with fireId
  let searchParameters = {
    q: '*',
    filter_by: `fireId:=${post.id}`
  }
  return TsenseClient.collections(Collection).documents().search(searchParameters)
})
.then((data) => {
  console.log(data);
  return data
}) 

You can read more about it https://medium.com/@justintulk/flattening-nested-promises-in-javascript

about 4 years ago · Juan Pablo Isaza Report

0

Instead of using then on the TsenseClient promise, you should return the promise instead and it will then-able like the following:

db.collection('posts').doc(post.id)
  .update({
    caption: post.caption
  })
  .then((data) => {
    // tsense collect all with fireId
    let searchParameters = {
      q: '*',
      filter_by: `fireId:=${post.id}`
    }
    return TsenseClient.collections(Collection).documents().search(searchParameters)
  })
    .then((data) => {
      console.log(data);
      return data
    })
    .then((tsenses) => {
      // tsense update all with fireId
      let tsenseUpdates = []
      tsenses.hits.forEach((hit) => {
        let doc = {
          "caption": post.caption
        }
      tsenseUpdates.push(TsenseClient.collections(Collection).documents(hit.id).search(searchParameters))
      })
      return Promise.allSettled(tsenseUpdates)
    })
    .then((tsenseUpdates) => {
      response.send('Update done.')
    })
  .catch((err) => {
    console.log(err);
  })
about 4 years ago · Juan Pablo Isaza Report

0

consider using async await you can find the docs for the async function on the mdn => https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

async function foo() {
    const p1 = new Promise((resolve) => setTimeout(() => resolve('1'), 1000))   const p2 = new Promise((_,reject) => setTimeout(() => reject('2'), 500))
  const results = [await p1, await p2] // Do not do this! Use Promise.all or Promise.allSettled instead.}
foo().catch(() => {}) // Attempt to swallow all errors.

code snippet example img

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!