/** Promise class shim with basic functionality. @ignore internal use. */ export interface PromisyClass { new < Result > ( handler ) : PromiseLike < Result > ; resolve < Result > ( result ) : PromiseLike < Result > ; reject ( err ) : PromiseLike < any > ; } /** Call func and return a promise for its result.
  * Optionally call given resolve or reject handler when the promise settles. */ export function tryFinally < Result > ( func , onFinish , Promise , resolve ? , reject ? ) { let promise ; try { promise = Promise . resolve ( func ( ) ) ; } catch ( err ) { promise = Promise . reject ( err ) ; } promise . then ( onFinish , onFinish ) ; if ( resolve ) promise . then ( resolve , reject ) ; return ( promise ) ; } /** Task wraps a promise, delaying it until some resource gets less busy. */ export class Task < Result > { constructor ( private func , private Promise , public stamp ) { } /** Wrap task result in a new promise so it can be resolved later. */ delay ( ) { if ( ! this . promise ) { this . promise = new this . Promise ( ( resolve , reject ) => { this . resolve = resolve ; this . reject = reject ; } ) ; } return ( this . promise ) ; } /** Start the task and call onFinish when done. */ resume ( onFinish ) { return ( tryFinally ( this . func , onFinish , this . Promise , this . resolve , this . reject ) ) ; } private promise : PromiseLike < Result > ; private resolve : ( result : Result ) => void ; private reject : ( err : any ) => void ; }