BruceWayne BruceWayne - 1 year ago 99
AngularJS Question

wait for a function result in a for loop

I'm using AngularJs and the module ngSweetAlert, i have to wait for the execution of instructions in the confirmation button function in a for loop :

for (var i = 0; i < myArray.length; i++) {
title: "Are you sure?",
text: "Your will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
}, function(isConfirm) {
if (isConfirm) {
// Instructions using myArray
} else {


Answer Source

EDIT: Original polling example was incorrect, as pointed out by Andrew Luhring

Here's how you could use the asynchronous callback style to schedule each alert popup after the former finishes:

function showAlertNTimes(n) {
    if (n > 0) {
        SweetAlert.swal({...}, function() {
            // Do confirmation stuff 
            showAlertNTimes(n - 1); 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download