export default function waitUntil < T > ( promiseFactory , delayMillis = 100 , maxAttempts = 30 ) { let nAttempts = 0 ; let giveItATryPromiseFactory = ( ) => promiseFactory ( ) ; const reschedule = ( error ) => { nAttempts ++ ; if ( nAttempts >= maxAttempts ) { return Promise . reject ( error ) ; } return new Promise ( ( resolve ) => setTimeout ( resolve , delayMillis ) ) . then ( ( ) => giveItATryPromiseFactory ( ) ) ; } ; giveItATryPromiseFactory = ( ) => promiseFactory ( ) . then ( ( data ) => { return Promise . resolve ( data ) ; } ) . catch ( reschedule ) ; return giveItATryPromiseFactory ( ) ; }