Cancel Iteration in Storage.forEach


#1

I want to cancel the iteration through the storage once I have found enough matches according to my predicates.

I ve assumed I could throw an error in that case and catch that out side like this:

 private async materializeByIteration(....) {

   let matches: T[] = [];

   let storage = ...

   try {

     await storage.forEach((value, key: string) => {
          ...
          if (matches.length == maxMatches) {
            throw new SufficientMatchesException();
          }
     });

  } catch (error) {

    if (!(error instanceof SufficientMatchesException)){
      throw error;
    }

  }

  return matches;     

}

But this results in an “Uncaught SufficientMatchesException”. Why? Any solution to this?

Thanks


#2

Strange… A

if (matches.length == maxMatches) {
          return false;
}

seems to work now.