javascript - How to break out of a loop from a returned promise value? -


a bit new javascript, i've read, values within promise usable within promise right?

basically getsomething() returns true/false promise. , break out of loop if true. tried this, i'm sure it's not right it's not printing out "breaking"

for(...) {     var bool = this.getsomething(a,b,c).then((flag) => {         if (flag == true) {             console.log('returning true');             return true; // can't use break have set boolean - eslint gives me unsyntactical break         }     });      if (bool == true) {         console.log('breaking');         break;     } }  getsomething(a,b,c) {     const n = b[a].element(by.binding('something'));     return n.gettext().then((text) => {         if (text === c) {         return clicksomething();        // returns true after click()         }     }); } 

the reason i'm using loop because need find matching text in strong tag, click on button in td tag below it.

<tbody>     <tr ng-repeat="something">         <td>             <strong>{{x.name}}</strong>         </td>         <td>             <button>{{x.button}}</button>         </td>     </tr> </tbody> 

there promises involved, protractor adds control flow queue, cannot read , handle loop + break in synchronous "top bottom" code.

the common solution problem use recursion+closures, see sample solutions here:


the reason i'm using loop because need find matching text in strong tag, click on button in td tag below it.

one option use by.xpath() locator:

element(by.xpath("//td[strong = '" + label + "']/following-sibling::td/button")).click(); 

where label strong value looking for.

or, more protractor-specific approach use filter():

var rows = element.all(by.repeater("something"));  var filteredrow = rows.filter(function (row) {     return row.element(by.binding("x.name")).gettext().then(function (name) {         return name === label;      }); }).first();  filteredrow.element(by.binding("x.button")).click(); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -