简体   繁体   English

异步/等待总是返回未定义

[英]async/await is always returning undefined

In my protractor app, I have the below method which is supposed to return boolean value, depending if it finds the element or not. 在我的量角器应用程序中,我具有以下方法,该方法应返回boolean值,具体取决于是否找到该元素。

async getTheValueOfEntery(entery: string) {
  var value = await element(by.xpath(entery))
    .isPresent().then((isExist) => {
      isExist;
    });
  return value;
}

But, the problem is, it always rerturns undefined , although i am sure, it should return true . 但是,问题是,它总是返回undefined ,尽管我确定它应该返回true

So, what is wrong in my method? 那么,我的方法有什么问题呢?


Update: 更新:

Indeed, i need to have a chain of calling, fat functions, so the most complete version of my function is as below: 确实,我需要有一连串的调用胖函数,因此,我的函数的最完整版本如下:

async getTheValueOfEntery(entery: string) {

  var value = await element(by.xpath(entery))
    .isPresent().then((isExist, entery: string) => {
      isExist ? element(by.xpath(entery)).getText() : 0;
    });

  return value;
}

But, i am not able to pass entery:string to the second lambda. 但是,我无法将entery:string传递给第二个lambda。

Pay attention to this: 注意这一点:

then((isExist)=>{isExist;});

In JavaScript fat arrow functions, if you embrace your code with curly brackets, then you need to return a value: 在JavaScript粗箭头功能中,如果用大括号括起来的代码,则需要返回一个值:

const sum = (a, b) => {
  return a + b;
}

Instead, if you only have a row, you can drop both curly brackets and return statement: 相反,如果只有一行,则可以放两个大括号并return语句:

const sum = (a, b) = a + b;

So, also in your case, you can decide if to add return before isExist , or if to remove the curly brackets. 因此,同样在您的情况下,您可以决定是在isExist之前添加return ,还是删除大括号。

But let's go further, if you only have a parameter, you can also drop round brackets: 但是,让我们走的更远,如果您只有一个参数,也可以删除圆括号:

const square = x => x * x;

So your code would look like this: 因此,您的代码如下所示:

.then(isExist => isExist); .then(isExist => isExist);

But this doesn't mean much! 但这并不意味着什么! You are just returning the parameter that you get in input, and it won't change your promise value. 您只是返回输入中得到的参数,它不会更改您的promise值。

In the end, you can simplify your code in this way: 最后,您可以通过以下方式简化代码:

async getTheValueOfEntery(entery:string){
    return (await element(by.xpath(entery))).isPresent();
}

Ps. PS。 It's preferred to talk about fat-arrow functions in javascript, rather than lambda! 最好谈论javascript中的fat-arrow函数,而不是lambda!

You don't return anything from your arrow function, so the resul of it is undefined . 您没有从arrow函数返回任何内容,因此其结果undefined If you write: 如果您写:

.isPresent().then((isExist, entery: string) => {
  isExist ? element(by.xpath(entery)).getText() : 0;
});

then it is as if you would write: 那么就好像您会写:

.isPresent().then(function (isExist, entery: string) {
  isExist ? element(by.xpath(entery)).getText() : 0;
});

So you need to add a return : 因此,您需要添加return

.isPresent().then((isExist, entery: string) => {
  return isExist ? element(by.xpath(entery)).getText() : 0;
});

Or remove the {} : 或删除{}

.isPresent().then((isExist, entery: string) => isExist ? element(by.xpath(entery)).getText() : 0);

And if you don't do anything with the value variable except of returning it, then you don't need to use await . 而且,如果除了返回value变量外,不对value变量执行任何操作,则无需使用await You could just write: 您可以这样写:

async getTheValueOfEntery(entery: string) {

  return element(by.xpath(entery))
    .isPresent().then((isExist, entery: string) => {
      isExist ? element(by.xpath(entery)).getText() : 0;
    });
}

The async would then in theory also not required, but it does not harm and it will show that this function will always return a Promise. 从理论上讲, async也不是必需的,但它不会造成危害,它将表明此函数将始终返回Promise。

The return from await is really the then param. await的回报确实是then参数。 You don't need the then at all. 您根本不需要then The result of the then is what is returning undefined . 的结果then是什么归国undefined

var value = await (element(by.xpath(entery))).isPresent()
return value;

Also, I am not sure what is async here. 另外,我不确定这里什么是异步的。 You might be awaiting the wrong object. 您可能正在等待错误的对象。

var value = await (element(by.xpath(entery))).isPresent()
return value;

or 要么

var ele = await element(by.xpath(entery))
return ele.isPresent()

or 要么

var ele = await by.xpath(entery)
return element(ele).isPresent()

updated 更新

var isExist = await element(by.xpath(entery))
        .isPresent()
var value = isExist ? await element(by.xpath(entery)).getText() : 0;
        });

      return value;

It's still not clear what you are awaiting though. 虽然还不清楚您在等待什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM