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

80
Views
Structure promise chain

Is there any way to better structure the code? In particular how exceptions are handled within a "promise chain"?

$("#save").click(function(e) {
    e.preventDefault();

    let $self = $(this);
    let profile = {}

    $self.prop("disabled", true)

    profile['oldPassword'] = $('#oldPassword').val();
    profile['newPassword'] = $('#newPassword').val();

    Profile.validateForm(profile).then(() => {
        Profile.sendAjax(profile).then(() => {
            $('#oldPassword').val('');
            $('#newPassword').val('');
            $("#profileAlert").text('Password changed successully').removeClass('alert-danger').addClass('alert-success').show().delay(5000).fadeOut()
        }).catch(e => {
            $("#profileAlert").text(e).removeClass('alert-success').addClass('alert-danger').show()
        }).finally(() => {
            $self.prop("disabled", false)
        })
    }).catch(e => {
        $("#profileAlert").text(e).removeClass('alert-success').addClass('alert-danger').show()
    }).finally(() => {
        $self.prop("disabled", false)
    })
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Given your catch and finally callbacks behave the same, you can safely chain the promises:

Profile.validateForm(profile).then(() => {
  return Profile.sendAjax(profile);
}).then(() => {
  $('#oldPassword').val('');
  $('#newPassword').val('');
  $("#profileAlert").text('Password changed successully').removeClass('alert-danger').addClass('alert-success').show().delay(5000).fadeOut()
}).catch(e => {
  $("#profileAlert").text(e).removeClass('alert-success').addClass('alert-danger').show()
}).finally(() => {
  $self.prop("disabled", false)
})
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!