0

如何访问 javascript 中 .then() 承诺内的函数返回值

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

1

你可以使用 async...await 语法来解决这个问题。


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

于 2021-11-13T08:10:17.113 回答
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 回答
0

如果您希望前端随时了解验证状态,则必须在此处调用 API 示例

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 });
  });
});
 
于 2021-11-13T12:07:52.360 回答
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) => {
            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
      });
于 2021-11-13T08:15:42.150 回答