0

javascript の .then() promise 内にある関数の戻り値にアクセスするにはどうすればよいですか

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 ]
4

4 に答える 4

0

それはあなたが考えているのと同じ仕事をします..

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

于 2021-11-13T09:05:06.190 に答える