Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS chained promises and finally callback

Tags:

angularjs

So I have 2 promise functions. When there is an error for the first function I want it to display an error message. When either complete or fail I would like them to execute a finally catch all function but for some reason it isn't working. my code looks like this:

// If our garment has a logo
shared.logoExists(garment, model).then(function () {

    // Save our panel
    return shared.save(model);

// If there was an error
}, function () {

    // Display an error message
    toastr.warning(localization.resource.logoPlacementError.message);

// Always close the library
}).finally(function () {

    // Reset our attachments
    self.resetAttachment();

    // Hide our library
    self.closeLibrary();
});

So basically what I am trying to achieve is if the first function fails, it will display and error. When the second function fails, it won't do anything. But if either succeed or fail, it will always close the library.

Does anyone know how I can achieve this?

like image 202
r3plica Avatar asked Dec 30 '25 06:12

r3plica


1 Answers

You have to use .catch after closing the then function:

// If our garment has a logo
shared.logoExists(garment, model).then(function () {

    // Save our panel
    return shared.save(model);

// If there was an error
}).catch(function () {

    // Display an error message
    toastr.warning(localization.resource.logoPlacementError.message);

// Always close the library
}).finally(function () {

    // Reset our attachments
    self.resetAttachment();

    // Hide our library
    self.closeLibrary();
});
like image 81
Jaime Still Avatar answered Jan 02 '26 05:01

Jaime Still



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!