How can I use then() in a queue of SweetAlert2 popups?

1.4k Views Asked by At

If I have a SweetAlert2 popup like this:

Swal.fire({
    title: 'Warning',
    icon: 'warning',
    text: 'This is a warning',
    confirmButtonText: 'Continue',
    cancelButtonText: 'Back',
    showCancelButton: true,
    reverseButtons: true,
}).then((result) => {
    if (result.value) {
        // Do stuff
    }   
});

And instead of having it fire right away, I want to add it to a queue like this:

// Create array
swal_queue = []; 

// Add popups to array
swal_queue.push({
        title: 'Warning',
        icon: 'warning',
        text: 'This is a warning',
        confirmButtonText: 'Continue',
        cancelButtonText: 'Back',
        showCancelButton: true,
        reverseButtons: true,
});

p = Swal.queue(swal_queue);
p.then(function() {swal_queue = []});

How do I add the .then() code to the queue for that popup?

1

There are 1 best solutions below

0
On

I think you can wrap the swal_queue as a subscriblable queue and then subscrible on the queue, once a new item comes in popup the dialog or alert.