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

187
Views
how to bypass multiple then in of mongoose

I Don't want to make checks as if (!isPresent) then do this ....

Is their any way to bypass all the then when my work is complete

var isPresent=false;
    myPromise
            .then((employee) => {
                    if (employee) {
                        // throw 'employee already exist';
                        isPresent = true;
                        return res.data(mapper.toFullModel(employee));

                    }
                return model;
            });
            .then(model => {
                if (!isPresent) {
                    return new db.employee(model).save();
                }
            })
            .then(employee => {
                if (!isPresent) {
                    employee.token = auth.getToken(employee);
                    return employee.save();
                }
            })
            .then(employee => {
                if (!isPresent) {
                    res.data(mapper.toFullModel(employee));
                }
            })
            .catch(err => {
                res.failure(err);
            });
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can rewrite your promise chain so the second part of it gets nested:

myPromise.then(employee => {
  if (employee) {
    return res.data(mapper.toFullModel(employee));
  }
  return new db.employee(model).save().then(employee => {
    employee.token = auth.getToken(employee);
    return employee.save();
  }).then(employee => {
    return res.data(mapper.toFullModel(employee));
  });
}).catch(err => {
  res.failure(err);
});
over 4 years ago · Santiago Trujillo 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!