Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
Access return value inside a nested if else in .then() Promise in javascript with axios

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
  });
console.log(verification)   // this returns undefined  
return verification     // this returns [ object promise ]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

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
};

about 4 years ago · Juan Pablo Isaza Denunciar

0

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
      });
about 4 years ago · Juan Pablo Isaza Denunciar

0

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;
  }
});

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda