2

I have a AddContactForm form that allows the user to add contacts.

When the user fills in the conactNumber- onBlur it checks if this conactNumber already exists.

How can I make The CheckIfContactExists function returns either true or false instead of the promise object?

Please note that I can't change the returned value from the api, it only return a contact object.

export default class AddContactForm extends Component {
  state = {
   ...
  };

  checkContact = () => {
    const { contactNumber } = this.state.newContactInfo;
    CheckIfContactExists(contactNumber); //return promise
  };

 render() {
   ...
    return (  
       ...
   );
  }
}

const CheckIfContactExists = async searchString => {
  const { data: contactsInfo } = await axios.get(`api/Contacts/SearchContact?contactNum=${searchString}`);
};
1
  • you can't, that's the thing with asynchronous calls they return promise, you can make it so promise resovles into boolean, but it will still be a promise Commented Feb 17, 2019 at 18:51

2 Answers 2

1

You can't make it return just a boolean since it's an asynchronous operation. You could make the checkContact function async as well and await it.

Example

export default class AddContactForm extends Component {
  state = {
   // ...
  };

  checkContact = async () => {
    const { contactNumber } = this.state.newContactInfo;
    const contactInfo = await CheckIfContactExists(contactNumber);

    this.setState({
      contactNumberTaken: Object.keys(contactInfo).length !== 0
    });
  };

  render() {
    // ...
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, but Boolean(contactsInfo) will always result true, An object that doesn't have any properties assigned is not considered "empty".
@user3378165 That's true. I updated the answer with a check to see if it's an empty object that was returned.
1

Make use of async await in checkContact just like you did for CheckIfContactExists. Also return the boolean result from CheckIfContactExits method

export default class AddContactForm extends Component {
  state = {
   ...
  };

  checkContact = async () => {
    const { contactNumber } = this.state.newContactInfo;
    try {
      const res = await CheckIfContactExists(contactNumber); 
      return res;
    } catch (e) {
       console.log('Error', error);
    }
  };

 render() {
   ...
    return (  
       ...
   );
  }
}

const CheckIfContactExists = async searchString => {
  const { data: contactsInfo } = await axios.get(`api/Contacts/SearchContact?contactNum=${searchString}`);
  if (Object.keys(contactsInfo).length > 0) {
     return true;
  } else {
      return false;
  }
};

2 Comments

Thank you for your answer, but if (contactsInfo) will always result true, An object that doesn't have any properties assigned is not considered "empty".
In that case you could simply check for size of object keys. My assumption when I wrote the answer was that the contactsInfo is returned undefined or null if not found

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.