tilgovi commented on issue #81:
URL: 
https://github.com/apache/incubator-annotator/issues/81#issuecomment-674584671


   I propose we start by making some attempt to have core pieces of our 
reusable code allow for synchronous iterators, but keep the asynchronous 
consumer API.
   
   I started experimenting with something like this, that we could use in our 
definitions for cartesian product or our matcher functions.
   
   ``` typescript
   type AbstractIterable<T> = AsyncIterable<T> | Iterable<T>;
   
   export function isAsyncIterable<T>(
     iterable: AbstractIterable<T>,
   ): iterable is AsyncIterable<T> {
     return typeof iterable[Symbol.asyncIterator] === 'function';
   }
   
   export function makeAsync<T>(iterable: AbstractIterable<T>): 
AsyncIterable<T> {
     if (isAsyncIterable<T>(iterable)) {
       return iterable;
     }
   
     return {
       [Symbol.asyncIterator]() {
         const iter = iterable[Symbol.iterator]();
         return {
           next() {
             return Promise.resolve(iter.next());
           },
         };
       },
     };
   }
   ```


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to