title: How to return an Array from a function in TypeScript with Protractor?
body: <p>I have the below code where I need to fill up an array with the drop list elements and return that array from the function so that I assert that array from the jasmine test case. </p>
  
  <pre><code>getAllCategoryName(): string[]{
  
          var usageCategoryFromPage: string[] =  [];
  
          E2EUtil.click(this.usageCategoriesPage.pageinationDropDownBtn);
  
          E2EUtil.click(this.usageCategoriesPage.highestPageRecords);
  
          element.all(by.xpath("//tbody[@class='ui-datatable-data ui-widget-content ui-datatable-hoverable-rows']/tr[*]/td[1]"))
              .each(function (element, index) {
  
                  element.getText().then(function (text){
                      usageCategoryFromPage.push(text);
                  })
              })
  
              .then(function(){
  
                  console.log("Size of the array from inside the then block " + usageCategoryFromPage.length);
  
                  usageCategoryFromPage.forEach(element =&gt; {
                       console.log("Usage Category Elements from inside the the function " + element);
                  });
  
                  return usageCategoryFromPage; // size here is 18
              });
  
          console.log("Usage Category size after the then block "  +usageCategoryFromPage.length)
          usageCategoryFromPage.forEach(element =&gt; {
                console.log("From Usage Category Page outside the then function" + element);
          });
  
          return usageCategoryFromPage; // size here is 0
      }
  </code></pre>
  
  <p>The problem is that the <code>usageCategoryFromPage</code> array is being returned as 0 outside the then block.</p>
  
  <p>Jasmine test case looks like :</p>
  
  <pre><code>it('Validate the Usage Category droplist values matches with the Usage Categories Table',() =&gt; {
  
          usageCategoriesPage.navigateTo();
          let allCategoryName = usageCategoriesPage.getAllCategoryName();
          allCategoryName.forEach(element =&gt; {
              console.log("Array elements printed from the test case " + element);
          });
  </code></pre>
  
  <p>Can any one please help?</p>