/**
 * Copyright (c) 2018-present, heineiuo.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ export default class IteratorHelper { static wrap < T > ( iterator , callback ) ; static wrap < T > ( iterator , callback ) ; static wrap < T > ( iterator , callback ) { if ( Symbol . iterator in iterator ) { const it = iterator as IterableIterator < T > ; it . return = ( ) : IteratorResult < T > => { const value = it . next ( ) . value ; callback ( value ) ; return { done : true , value } ; } ; return it ; } iterator . return = async ( ) : Promise < IteratorResult < T > > => { try { const value = ( await iterator . next ( ) ) . value ; callback ( value ) ; return { done : true , value } ; } catch ( e ) { callback ( e ) ; return { done : true , value : e } ; } } ; return iterator ; } static makeAsync < T > ( iterator ) { const asyncIter = { return : async ( value ? ) : Promise < IteratorResult < T > > => { try { const value = iterator . next ( ) . value ; return { done : true , value } ; } catch ( e ) { return { done : true , value : e } ; } } , next : async ( value ? ) : Promise < IteratorResult < T > > => { return iterator . next ( ) ; } , } as AsyncIterableIterator < T > ; return { next : asyncIter . next , [ Symbol . asyncIterator ] ( ) { return asyncIter ; } , } ; } }