import { AsyncIterableX } from './asynciterablex' ; import { AsyncSink } from './asyncsink' ; import { memoize } from './operators/memoize' ; template TSource param { Function } func returns { ( ... args : any [ ] ) => AsyncIterableX < TSource > } export function asyncifyErrback < TSource > ( func ) { return function ( ... args ) { const sink = new AsyncSink < TSource > ( ) ; const handler = function ( err , ... innerArgs ) { if ( err ) { sink . error ( err ) ; sink . end ( ) ; } else { sink . write ( innerArgs . length === 1 ? innerArgs [ 0 ] : innerArgs ) ; sink . end ( ) ; } } ; try { func ( ... args . concat ( handler ) ) ; } catch ( e ) { sink . error ( e ) ; sink . end ( ) ; } const yielder = async function * ( ) { for ( let next ; ! ( next = await sink . next ( ) ) . done ; ) { yield next . value ; } } ; return memoize < TSource > ( ) ( yielder ( ) ) ; } ; }