declare global { interface Array < T > { someAsync ( cond ) : Promise < boolean > ; everyAsync ( cond ) : Promise < boolean > ; forEachAsync ( cb ) : Promise < void > ; mapAsync < R > ( cb ) : Promise < R [ ] > ; } } export function someAsync < T > ( this , cond ) { return AsyncHelpers . someAsync ( this , cond ) ; } export function everyAsync < T > ( this , cond ) { return AsyncHelpers . everyAsync ( this , cond ) ; } export function forEachAsync < T > ( this , cb ) { return AsyncHelpers . forEachAsync ( this , cb ) ; } export function mapAsync < T , R > ( this , cb ) { return AsyncHelpers . mapAsync ( this , cb ) ; } export function setTimeoutAsync ( ms ) { return new Promise < void > ( resolve => { setTimeout ( resolve , ms ) ; } ) ; } export function setTimeoutFramesAsync ( frames ) { return new Promise ( resolve => { let left = frames || 0 ; const cb = ( ) => { if ( -- left <= 0 ) { resolve ( ) ; } else { requestAnimationFrame ( cb ) ; } } ; cb ( ) ; } ) ; } export const AsyncHelpers = { async someAsync < T > ( arr , cond ) { for ( let i = 0 ; i < arr . length ; ++ i ) { const ok = await cond ( arr [ i ] , i , arr ) ; if ( ok ) { return true ; } } return false ; } , async everyAsync < T > ( arr , cond ) { for ( let i = 0 ; i < arr . length ; ++ i ) { const ok = await cond ( arr [ i ] , i , arr ) ; if ( ! ok ) { return false ; } } return true ; } , async forEachAsync < T > ( arr , cb ) { for ( let i = 0 ; i < arr . length ; ++ i ) { await cb ( arr [ i ] , i , arr ) ; } } , async mapAsync < T , R > ( arr , cb ) { const results = [ ] ; for ( let i = 0 ; i < arr . length ; ++ i ) { const r = await cb ( arr [ i ] , i , arr ) ; results . push ( r ) ; } return results ; } , } ; if ( ! process . env . SKIP_ASYNC_PROTO_EXTEND ) { Array . prototype . someAsync = someAsync ; Array . prototype . everyAsync = everyAsync ; Array . prototype . forEachAsync = forEachAsync ; Array . prototype . mapAsync = mapAsync ; }