passing a value from didOpen sweetalert2 parameter to the .then

5.5k Views Asked by At

I already have the didOpen parameter built and implemented a JSON object with values inside of it.

The question is... How can I pass that JSON object to the then part of the Swal.fire({ }) .then arrow function

Swal.fire({
  html: ` HERE IS ALL THE ID's STUFF `,
  didOpen() {
    let itemPOST = {
      /* the json i trying to catch */ }

    // do bunch of stuff and save it into itemPOST

  } //==>end of didOpen
}).then(() => {
  //console log here for itemPOST
})
1

There are 1 best solutions below

0
On BEST ANSWER

the solution is to include some variable after the swal, pass the value to that variable and call it in the .then() arrow function

let giveMeTheValue;  //<==    variable to receive the value
Swal.fire({
  html: ` HERE IS ALL THE ID's STUFF `,
  didOpen() {
    let itemPOST = { /* the json with data */ }

    // do bunch of stuff and save it into itemPOST

    giveMeTheValue = itemPOST   //<== giving the data to pass to then()
  } 
}).then(() => {
  console.log(giveMeTheValue)
})