Breaking out of a Protractor .filter() or .map() loop
<p>I'm using Protractor and cucumber framework; how do I break out of a .filter or .map loop? I do not want to continue to iterate further if I found a match!</p>

<pre><code>Page.prototype.getElementByKey = function (key) {
      var foundElement = null;
      return someElement.all(by.css('.someClass')).map(function (rawItem, index) {
        var itemObject = new ItemObjectClass(rawItem);
        return itemObject.getItemKey().then(function (foundItemKey) {
          var matched = String(foundItemKey).trim() === String(key).trim();

         console.log(' Matched: { ' + matched + ' }  index {'+index+'}');
          //if we have a match break out of the .filter function
          if (matched) {
            foundElement = itemObject;
            throw new Error("Just our way of breaking out of .filter() above");
          }
        });
      }).then(function () {
        //callback
        throw new Error('\n!!!!!Callback should not be called; 
       this means that we could not find an element that matched the passed in key above');
      }, function (error) {
        //error
        console.log('\n*******************errorCallback was called; '+error);
        return foundElement;
      });
    };
</code></pre>

<p>The above code finds the element but continues to iterate until the end instead of stopping when there's a match and breaking out by calling the <strong>errorCallback</strong> function. </p>

<p>Given that .map function returns "<strong><em>a promise that resolves to an array of values returned by the map function</em></strong>" <a href="http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.map" rel="nofollow">http://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.map</a>, I'm taking advantage of the fact that a promise will call its errCallback if the promise cannot be resolved. </p>

<p>By throwing an a fake error, the <strong><em>errorCallback</em></strong> should be called and thereby break out of the .map loop.</p>

<p>Unfortunately, it successfully throws the error but continues with the loop instead of breaking out. I know that because when I</p>

<blockquote>
  <p>console.log("boolean "+matched+" and index "+index);</p>
</blockquote>

<p>I get this: </p>

<pre><code>matched: false index: 0
matched: false index: 1
matched: true index 2 //it should have stopped here since matched = true
matched false index 3 // this should NOT have printed
</code></pre>

<p>so breaking out isn't working any ideas?</p>