【问题标题】:How to return a boolean in a promise?如何在承诺中返回布尔值?
【发布时间】:2020-05-11 01:51:25
【问题描述】:

在 submitButton 事件中,我想检查是否有用户注册。

假设有 5 个用户注册:

 getUsers = function () {
    return new Promise(function (resolve, reject) {
      resolve(5);
      reject(0);
    });
  }

在 checkregisteredUsers 中,我调用 getUsers,然后检查是否有任何用户。如果有任何用户,则返回 true,否则返回 false。我设置了 UsersFound 的状态。

  checkRegisteredUsers = () => {
    return this.getUsers().then(result => {
      return this.setState({ UsersFound: result > 0 ? true : false };
    }).catch(reason => {
      console.log(reason);
    });
  }

现在,在函数 submitButton 中,我想检查是否有注册用户。

submitButton = () => {
      this.checkRegisteredUsers().then(result => {
      this.setState({ UsersFound: result > 0 ? true : false });
    });

    if(!this.state.UsersFound){
    return; **//exit code here and the rest of the code below should not execute and show the dialog box**
    }
    // run some other code
  }

当我调用 submitButton 时,尚未设置 UsersFound 的状态。如果我再次单击它,则它已设置。 我怎样才能将它设置在一个承诺中?或者如何使用 Promise 在 submitButton 中检查真/假?

编辑:

问题是:我如何从 Promise 中返回布尔值,因为我想检查如下内容:

submitButton = () => {
 if(this.checkRegisteredUsers()){
  return; //the rest of the code below should not execute
}
 //if check users is true then the code below should not execute
 // some other code
}

我不想返回一个承诺,我想要一个布尔值。

【问题讨论】:

  • 然后把逻辑放进去
  • checkRegisteredUsers 方法中删除thencatch 回调。我建议也使用asyn/await 语句来处理异步函数。
  • 您是这样建议的吗:checkRegisteredUsers = () => { return this.getUsers(); } 我仍然无法设置 userChecks 的状态。我第一次点击按钮时它仍然返回 false

标签: javascript reactjs promise


【解决方案1】:

长话短说:你不能让一个返回承诺的函数返回一个布尔值。如果您有一些异步操作(例如从某个服务获取用户的获取),那本质上是异步的。您不能让这样的函数返回布尔值,因为当函数本身结束时,您没有返回 true 或 false 的信息。

最接近的方法是使用 async/await。但是异步函数仍然只返回Promise 对象。你可以使用 async/await 让它看起来不同,但它只是语法糖。您仍然有效地运行await 之后的所有代码,就好像它是传递给then 的函数一样。

举个例子:

//Some function that will resolve or reject.
const getUsers = () {
  return new Promise(function (resolve, reject) {
    resolve(5);
  });
}
const foundUsers = async () => {
  const users = await getUsers();
  return Boolean(users);
}
const LoggedIn = () => {
  const [usersFound, setUsersFound] = useState(false)
  const onSubmit = async () => {
    const found = await foundUsers();
    if (found) {
      //do stuff
    }
    setUsersFound(found)
  }
  return (
    <>
      <button onClick={onSubmit}>Submit</button>
      { usersFound ? <Dialog /> : <WhateverElse /> }
    </>
  )
}

和这个是一样的:

const LoggedIn = () => {
  const [usersFound, setUsersFound] = useState(false)
  const onSubmit = () => {
    foundUsers().then((found) => {
      if (found) {
        //do stuff
      }
      setUsersFound(found)
    })
  }
  return (
    <>
      <button onClick={onSubmit}>Submit</button>
      { usersFound ? <Dialog /> : <WhateverElse /> }
    </>
  )
}

【讨论】:

  • 目的是如果找到用户,则不执行其余代码 insde 提交按钮。我怎样才能做到这一点?这就是为什么我在这里尝试使用真假。
  • 我将答案替换为我认为可以更好地回答您所问的问题“我如何从承诺中返回布尔值”。希望这更有意义。
猜你喜欢
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
相关资源
最近更新 更多