Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在循环中处理承诺_Javascript_Loops_Promise_Protractor - Fatal编程技术网

Javascript 如何在循环中处理承诺

Javascript 如何在循环中处理承诺,javascript,loops,promise,protractor,Javascript,Loops,Promise,Protractor,我有一段代码如下: browser.sleep(2000).then(function(){ element.all(by.xpath('one xpath')).then(function(elements){ for(let elm of elements){ elm.getText().then(function(text){ if(text == "ButtonA"){ elm.click(); }

我有一段代码如下:

browser.sleep(2000).then(function(){
  element.all(by.xpath('one xpath')).then(function(elements){
   for(let elm of elements){
     elm.getText().then(function(text){
      if(text == "ButtonA"){
        elm.click();
      }
    })
   }
 })
});

funcB();
funcB()
elm.click()

我知道,我在一个for循环中使用了Promise。它会犯错误。我已经尝试了在internet上找到的一些解决方案,但到目前为止,我还没有找到如何使
elm.click()
funcB
运行之前工作。请帮忙

如果您使用
async/await
而不是
,您会惊讶地发现学生可以使用量角器

it('test case', async () => {
  await browser.sleep(2000)
  let elementArrayName = element.all(by.xpath('one xpath'));
  for (let i = 0; i<await elementArrayName.count(); i++) {
    let text = await elementArrayName.get(i).getText();
    if(text === "ButtonA"){
      await elementArrayName.get(i).click();
    }
  }

  // IMPORTANT!
  // I don't know what your funcB is, 
  // but I have a feeling it returns a promise and thus needs await too
  await funcB();
});
it('testcase',async()=>{
等待浏览器。睡眠(2000)
让elementArrayName=element.all(by.xpath('one-xpath');

对于(让i=0;i
Promise.all(element.all…)。然后(()=>funcB())
应该可以工作。
elem.click()
本身可能会做异步工作-您是否希望等待异步工作也完成?此外,我们不知道
elem
实际上是什么(可能是一个HTML按钮?)以及它是否支持承诺或回调,以在需要时发出通知done@Jamiec:elements=[Button,ButtonA,ButtonB,ButtonC,ButtonD]。我想先找到ButtonA,然后单击ButtonA,最后运行funcB()所以…把它放在循环之后。我不理解这种混乱。我想每个人在开始的时候都有这个问题。至少我也有。我不知道为什么所有这些反对票一直存在coming@MusterTester如果对你有效,请告诉我