beware, untested
Not familiar with swal, but after a quick look into it's source code it doesn't seem to provide promises, so I wrote a little wrapper (based on the src, on the linked demo-page):
//since swal doesn't implement promises on it's own, here a wrapper that returns one
function $swal(title, message, type){
return typeof message === "function"? $swal(title).then(message):
typeof title !== "object"? $swal({ title:title, message:message, type:type }):
config => $q(function(resolve){ window.swal(config, resolve) });
}
this should behave just like the regular swap-function just that it returns a promise. needs on $q
from angular and an initialized SweetAlert
now asyncromity get's easy:
myArray.reduce(function(prev, value, index, arr){
//wait till the previous promise has been resolved,
//then resolve the this one
return prev.then(function(){
//return prev.catch(/* error handling for prev */).then(function(){
//utilize the wrapped function.
return $swal({
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
//}).then(function(isConfirm){
}, function(isConfirm){ //this is just an alias for the previous line
console.log(index, isConfirm, value);
});
});
}, $q.resolve(true));
I pass in a resolved Promise, so I don't have to deal with wether it's the first or a dependant call.
reduce also wrapps the current index and value for each call.
One thing that's not handled yet, is if your code throws an error.
Edit: As 4castle pointed out in a comment, SweetAlert2 seems to implement promises.
Well, then you simply don't need the $swal-wrapper, and use the regular swal-function with reduce.