Home > Software engineering >  Access return value inside a nested if else in .then() Promise in javascript with axios
Access return value inside a nested if else in .then() Promise in javascript with axios

Time:11-13

How can I access the function return value that is within inside .then() promise in javascript

const verification = () => {
  twil.verificationChecks.create({
    to: phone,
    code: vcode
  }).then((verify) => {
    otp = verify.status; //twilio  
    // console.log(otp);
    const user = finduser;
    if (otp === "approved") { // otp approved

      if (user) { //check phone
        find(); //find user
        return "user found"
      } else {
        add(); //add user
        return "user added"

      } //check phone

    } // otp approved
  });
}

CodePudding user response:

You can use async...await syntax for this problem.


const verification = async () => {
    const verify = await twil.verificationChecks.create({
        to: phone,
        code: vcode,
    });

    otp = verify.status; //twilio
    // console.log(otp);
    const user = finduser;
    if (otp === 'approved') {
        // otp approved

        if (user) {
            //check phone
            find(); //find user
            return 'user found';
        } else {
            add(); //add user
            return 'user added';
        } //check phone
    } // otp approved
};

CodePudding user response:

Even you can use event emitter to tackle this problem

let verifyData;
      let ee = new EventEmitter(); //create new event to be called after data is available
      const verification = () => {
        twil.verificationChecks
          .create({ to: phone, code: vcode })
          .then((verify) => {
            verifyData = verify;
            ee.emit("dataFilled"); // Call event emitter
            otp = verify.status; //twilio
            // console.log(otp);
            const user = finduser;
            if (otp === "approved") {
              // otp approved

              if (user) {
                //check phone
                find(); //find user
                return "user found";
              } else {
                add(); //add user
                return "user added";
              } //check phone
            } // otp approved
          });
      };

      ee.on("dataFilled", function () {
        console.log(verifyData);
        // Add you code here
      });

CodePudding user response:

It will do the same work as you are thinking..

let verifyData;
let ee = new EventEmitter(); //create new event to be called after data is available
const verification = () => {
  twil.verificationChecks.create({ to: phone, code: vcode }).then((verify) => {
    otp = verify.status; //twilio
    // console.log(otp);
    const user = finduser;
    if (otp === "approved") {
      // otp approved
      if (user) {
        //check phone
        find(); //find user
        verifyData = "user found";
      } else {
        add(); //add user
        verifyData = "user added";
      } //check phone
      ee.emit("varificationDone"); // Call event emitter
    } // otp approved
  });
};

ee.on("varificationDone", function () {
  switch (verifyData) {
    case "user found":
      // your code here
      break;
    case "user added":
      // your code here
      break;
    default:
      break;
  }
});

CodePudding user response:

If you want your frontend to stay informed about the verification status, you have to call an API example here

app.post("/varification", (req, res) => {
  axios.post("URL", verify).then((response) => {
    console.log(response.data);
    // if your response.data is "user found"
    res.json({ varificationStatus: response.data });
  });
});
 
  • Related