0

I'm making a function to return a boolean to know if an element is present or not, but I get

this is the status: false

How can I get an only true or false response, please help, I'm using an if condition but it shows that response...

  async IsPresent(element){
        try{
            await element.isPresent().then(function(status){
                console.log("this is the status: " + status);
                return status; 
            });
        }
        catch(err){
            return console.log(err);
        }
    };

this is the function where I'm calling IsPresent

async ClickSomething(){
        try{
            await this.helpers.ClickElement(oneElement);

            if(await this.helpers.IsPresent(anotherElement) == false){
                //Do something
            }else{
               //Do something esle
        }
        catch(err){
            return console.log(err);
        }
    };
Alan Díaz
  • 43
  • 1
  • 8
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – VLAZ Mar 19 '20 at 07:06

1 Answers1

0

There is a lot of unnecessary code in that function. You could achieve the same functionality using the following

async IsPresent(element){
    try {
        return await element.isPresent();
    }
    catch (err) {
        return console.log(err);
    }
};

First thing to note is that when a function is declared async you are able to use the await keyword to pause the execution until that any associated promise is resolved. You are actually using two different approached to resolve promises here (promise.resolve and .then()) when only awaits are necessary.

Second thing to note is that isPresent return true or false anyway so that can be directly returned from your function.

DublinDev
  • 2,318
  • 2
  • 8
  • 31
  • Hello, no it didn't work, but I updated by function I can get the boolean response but when calling the function it doesn't recognize the boolean response, I updated the post above, thank you – Alan Díaz Mar 19 '20 at 21:45
  • Your new IsPresent function does not appear to return anything, which is why the Boolean value is not accessible when the function is called. What was the issue you encountered with what I posted? – DublinDev Mar 20 '20 at 09:04