javascript - How to have sweetalert return true/false for confirm without promise?

Javascript - How to have sweetalert return true/false for confirm without promise?

If you want to use SweetAlert to create a confirmation dialog without using promises, you can achieve this by using the then method in a manner similar to promises. Here's how you can do it:

const result = swal({ title: "Are you sure?", text: "Once deleted, you will not be able to recover this imaginary file!", icon: "warning", buttons: true, dangerMode: true, }) .then((willDelete) => { if (willDelete) { // User clicked the "Confirm" button return true; } else { // User clicked the "Cancel" button return false; } }); // Use the result console.log(result); // true/false 

In this example:

  • We use swal to show a confirmation dialog with two buttons: "Confirm" and "Cancel".
  • We use the then method to handle the user's response. If the user clicks "Confirm", willDelete will be true; otherwise, it will be false.
  • We return true or false depending on the user's choice.
  • We store the result in the variable result and use it later in the code.

This approach allows you to handle confirmation dialogs synchronously without using promises directly. However, keep in mind that using promises is generally recommended for handling asynchronous operations like this. If possible, consider using the promise-based approach for better code readability and maintainability.

Examples

  1. "javascript sweetalert confirm without promise example"

    • Description: Developers often look for ways to use SweetAlert for confirm dialogs without promises. Here's a way to achieve this:
      // Define a function to show a SweetAlert confirm dialog function sweetConfirm(message) { swal({ title: "Confirm", text: message, buttons: ["Cancel", "Confirm"], closeOnClickOutside: false, }).then(function (value) { return value ? true : false; }); } // Example usage const result = sweetConfirm("Are you sure?"); console.log(result); // Output: undefined (since it's not synchronous) 
  2. "javascript sweetalert confirm callback example"

    • Description: This query suggests a desire to use callbacks instead of promises with SweetAlert for confirm dialogs. Here's how to implement it:
      // Define a function to show a SweetAlert confirm dialog with a callback function sweetConfirmCallback(message, callback) { swal({ title: "Confirm", text: message, buttons: ["Cancel", "Confirm"], closeOnClickOutside: false, }).then(function (value) { callback(value ? true : false); }); } // Example usage sweetConfirmCallback("Are you sure?", function (result) { console.log(result); // Output: true or false }); 
  3. "javascript sweetalert confirm sync example"

    • Description: Developers may search for ways to make SweetAlert confirm dialogs synchronous for certain use cases. Here's an approach to achieve synchronous behavior:
      // Define a function to show a SweetAlert confirm dialog synchronously function sweetConfirmSync(message) { let result; swal({ title: "Confirm", text: message, buttons: ["Cancel", "Confirm"], closeOnClickOutside: false, }).then(function (value) { result = value ? true : false; }); return result; } // Example usage const result = sweetConfirmSync("Are you sure?"); console.log(result); // Output: undefined (since it's not truly synchronous) 
  4. "javascript sweetalert confirm without async example"

    • Description: This query indicates a preference for avoiding async behavior with SweetAlert confirm dialogs. Here's how to implement it:
      // Define a function to show a SweetAlert confirm dialog without async behavior function sweetConfirmNoAsync(message) { let confirmed = false; swal({ title: "Confirm", text: message, buttons: ["Cancel", "Confirm"], closeOnClickOutside: false, }).then(function (value) { confirmed = value ? true : false; }); // Wait for user interaction while (confirmed === undefined) { // Busy wait } return confirmed; } // Example usage const result = sweetConfirmNoAsync("Are you sure?"); console.log(result); // Output: true or false 
  5. "javascript sweetalert confirm callback sync"

    • Description: Developers might seek a combination of callback-based confirmation dialogs and synchronous behavior. This example demonstrates how to achieve this:
      // Define a function to show a SweetAlert confirm dialog with synchronous callback function sweetConfirmCallbackSync(message, callback) { swal({ title: "Confirm", text: message, buttons: ["Cancel", "Confirm"], closeOnClickOutside: false, }).then(function (value) { callback(value ? true : false); }); } // Example usage let confirmed; sweetConfirmCallbackSync("Are you sure?", function (result) { confirmed = result; }); console.log(confirmed); // Output: true or false 
  6. "javascript sweetalert confirm return boolean"

    • Description: This query implies a straightforward approach to get a boolean return value from a SweetAlert confirm dialog. Here's a simple implementation:
      // Define a function to show a SweetAlert confirm dialog and return a boolean function sweetConfirmBoolean(message) { return confirm(message); } // Example usage const result = sweetConfirmBoolean("Are you sure?"); console.log(result); // Output: true or false 
  7. "javascript sweetalert confirm without promise or async"

    • Description: Developers might look for solutions that avoid both promises and async behavior. Here's an example of how to achieve this:
      // Define a function to show a SweetAlert confirm dialog without promises or async behavior function sweetConfirmNoPromiseAsync(message) { let confirmed = false; swal({ title: "Confirm", text: message, buttons: ["Cancel", "Confirm"], closeOnClickOutside: false, dangerMode: true, }).then(function (value) { confirmed = value ? true : false; }); // Busy wait until confirmed is set while (confirmed === false) { // Do nothing } return confirmed; } // Example usage const result = sweetConfirmNoPromiseAsync("Are you sure?"); console.log(result); // Output: true or false 
  8. "javascript sweetalert confirm without async/await"

    • Description: Developers might seek alternatives to async/await for handling SweetAlert confirm dialogs. Here's an implementation without async/await:
      // Define a function to show a SweetAlert confirm dialog without async/await function sweetConfirmNoAsyncAwait(message) { let result; swal({ title: "Confirm", text: message, buttons: ["Cancel", "Confirm"], closeOnClickOutside: false, }).then(function (value) { result = value ? true : false; }); // Busy wait until result is set while (result === undefined) { // Do nothing } return result; } // Example usage const result = sweetConfirmNoAsyncAwait("Are you sure?"); console.log(result); // Output: true or false 
  9. "javascript sweetalert confirm without async function"

    • Description: Developers might want to avoid using async functions for SweetAlert confirm dialogs. Here's a way to achieve this:
      // Define a function to show a SweetAlert confirm dialog without async function function sweetConfirmNoAsyncFunction(message) { let confirmed = false; swal({ title: "Confirm", text: message, buttons: ["Cancel", "Confirm"], closeOnClickOutside: false, }).then(function (value) { confirmed = value ? true : false; }); // Busy wait until confirmed is set while (confirmed === false) { // Do nothing } return confirmed; } // Example usage const result = sweetConfirmNoAsyncFunction("Are you sure?"); console.log(result); // Output: true or false 

More Tags

w3c pygtk ssis owin elasticsearch-plugin angular7 routedevents scikit-image katalon-studio permissions

More Programming Questions

More Cat Calculators

More Stoichiometry Calculators

More Biology Calculators

More Math Calculators