Sami
Sami

Reputation: 413

run two Angular functions from the same popup window

Would you recommend somthinglike this if I need to run 2 functions, Assign1 and Assign2 from the same popup window? If either one gives an error, the window will close without saving anything. If Not too sure about syntax.

function Assign() {
    Assign1().then(function(isSuccess) {
        if (isSuccess) {
            Assign2();
            saveAndClose();
        } else {
            CloseAndShowError();
        }
    }, function() {
        CloseAndShowError();
    });

}

I should specify that Assign1 and Assign2 are made with promises in their definitions and I need to know if overall the calls are successful

Upvotes: 0

Views: 40

Answers (2)

Diego Marquez
Diego Marquez

Reputation: 492

Try $q.all(); https://docs.angularjs.org/api/ng/service/$q#all

This method allows you to run more than one promise as if it was a single one.

So, inside your Assign() method, there could be the following code.

let promisesToBeRunned = [Assign1, Assign2];
$q.all(promisesToBeRunned)
    .then(
        () => {doYourSuccessStuff();}),
        CloseAndShowError)
    );

If both promises are successful, the success callback will be executed.

If one of the promises, either Assign1 or Assign2 fails, the error callback will be called.

NOTE : Try to use $q.reject() on Assign1, so you won't have to use if (isSuccess). Maybe if you post the Assign1 definition, I could give you an idea.

I hope this helps.

Upvotes: 0

Saurabh Agrawal
Saurabh Agrawal

Reputation: 7739

You can try like this

function1() {
  //function1 body
}

function2() {
  //function2 body
}

In template call them as

<button (click)="function1();function2()"> </button>

Upvotes: 1

Related Questions