import Type from "typescript-dotnet-core/Types" ; import deferImmediate from "typescript-dotnet-threading/deferImmediate" ; import DisposableBase from "typescript-dotnet-core/Disposable/DisposableBase" ; import InvalidOperationException from "typescript-dotnet-core/Exceptions/InvalidOperationException" ; import ArgumentException from "typescript-dotnet-core/Exceptions/ArgumentException" ; import ArgumentNullException from "typescript-dotnet-core/Exceptions/ArgumentNullException" ; import ObjectPool from "typescript-dotnet-core/Disposable/ObjectPool" ; import Set from "typescript-dotnet-core/Collections/Set" ; import defer from "typescript-dotnet-threading/defer" ; import ObjectDisposedException from "typescript-dotnet-core/Disposable/ObjectDisposedException" ; import { Closure , Selector } from "typescript-dotnet-core/FunctionTypes" ; const VOID0 = void 0 , NULL = null , PROMISE = "Promise" , PROMISE_STATE = PROMISE + "State" , THEN = "then" , TARGET = "target" ; function isPromise < T > ( value ) { return Type . hasMemberOfType ( value , THEN , Type . FUNCTION ) ; } export type Resolver = Selector < TSDNPromise . Resolution < any > , any > | null | undefined ; function resolve < T > ( value , resolver , promiseFactory ) { let nextValue = resolver ? resolver ( value ) : value ; return nextValue && isPromise ( nextValue ) ? TSDNPromise . wrap ( nextValue ) : promiseFactory ( nextValue ) ; } function handleResolution ( p , value , resolver ? ) { try { let v = resolver ? resolver ( value ) : value ; if ( p ) { p . resolve ( v ) ; } return null ; } catch ( ex ) { if ( p ) { p . reject ( ex ) ; } return ex ; } } function handleResolutionMethods ( targetFulfill , targetReject , value , resolver ? ) { try { let v = resolver ? resolver ( value ) : value ; if ( targetFulfill ) targetFulfill ( v ) ; } catch ( ex ) { if ( targetReject ) targetReject ( ex ) ; } } function handleDispatch < T , TFulfilled = T , TRejected = never > ( p , onFulfilled , onRejected ? ) { if ( p instanceof PromiseBase ) { p . doneNow ( onFulfilled , onRejected ) ; } else { p . then ( < any > onFulfilled , onRejected ) ; } } function handleSyncIfPossible < T , TFulfilled = T , TRejected = never > ( p , onFulfilled , onRejected ? ) { if ( p instanceof PromiseBase ) return p . thenSynchronous ( onFulfilled , onRejected ) ; else return p . then ( onFulfilled , onRejected ) ; } function newODE ( ) { return new ObjectDisposedException ( "TSDNPromise" , "An underlying promise-result was disposed." ) ; } export class PromiseState < T > extends DisposableBase { constructor ( protected _state , protected _result ? , protected _error ? ) { super ( PROMISE_STATE ) ; } protected _onDispose ( ) { this . _state = VOID0 ; this . _result = VOID0 ; this . _error = VOID0 ; } protected getState ( ) { return this . _state ; } get state ( ) : TSDNPromise . State { return this . _state ; } get isPending ( ) : boolean { return this . getState ( ) === TSDNPromise . State . Pending ; } get isSettled ( ) : boolean { return this . getState ( ) != TSDNPromise . State . Pending ; } get isFulfilled ( ) : boolean { return this . getState ( ) === TSDNPromise . State . Fulfilled ; } get isRejected ( ) : boolean { return this . getState ( ) === TSDNPromise . State . Rejected ; } protected getResult ( ) { return this . _result ; } get result ( ) : T | undefined { this . throwIfDisposed ( ) ; return this . getResult ( ) ; } protected getError ( ) { return this . _error ; } get error ( ) : any { this . throwIfDisposed ( ) ; return this . getError ( ) ; } } export abstract class PromiseBase < T > extends PromiseState < T > implements PromiseLike < T > { protected constructor ( ) { super ( TSDNPromise . State . Pending ) ; this . _disposableObjectName = PROMISE ; } param onFulfilled param onRejected abstract doneNow ( onFulfilled , onRejected ? ) ; param onFulfilled param onRejected abstract thenSynchronous < TFulfilled = T , TRejected = never > ( onFulfilled , onRejected ? ) ; param onFulfilled param onRejected thenThis ( onFulfilled , onRejected ? ) { this . doneNow ( onFulfilled , onRejected ) ; return this ; } param onFulfilled param onRejected returns { TSDNPromise } then < TFulfilled = T , TRejected = never > ( onFulfilled , onRejected ? ) { this . throwIfDisposed ( ) ; return new TSDNPromise < TFulfilled | TRejected > ( ( resolve , reject ) => { this . doneNow ( result => handleResolutionMethods ( resolve , reject , result , onFulfilled ) , error => onRejected ? handleResolutionMethods ( resolve , reject , error , onRejected ) : reject ( error ) ) ; } ) ; } param onFulfilled param onRejected returns { TSDNPromise } thenAllowFatal < TFulfilled = T , TRejected = never > ( onFulfilled , onRejected ? ) { this . throwIfDisposed ( ) ; return new TSDNPromise < TFulfilled | TRejected > ( ( resolve , reject ) => { this . doneNow ( result => resolve ( < any > ( onFulfilled ? onFulfilled ( result ) : result ) ) , error => reject ( onRejected ? onRejected ( error ) : error ) ) ; } ) ; } param onFulfilled param onRejected done ( onFulfilled , onRejected ? ) { defer ( ( ) => this . doneNow ( onFulfilled , onRejected ) ) ; } param milliseconds returns delayFromNow ( milliseconds = 0 ) { this . throwIfDisposed ( ) ; return new TSDNPromise < T > ( ( resolve , reject ) => { defer ( ( ) => { this . doneNow ( v => resolve ( v ) , e => reject ( e ) ) ; } , milliseconds ) ; } , true ) ; } param milliseconds returns delayAfterResolve ( milliseconds = 0 ) { this . throwIfDisposed ( ) ; if ( this . isSettled ) return this . delayFromNow ( milliseconds ) ; return new TSDNPromise < T > ( ( resolve , reject ) => { this . doneNow ( v => defer ( ( ) => resolve ( v ) , milliseconds ) , e => defer ( ( ) => reject ( e ) , milliseconds ) ) ; } , true ) ; } param onRejected returns { PromiseBase < TResult > } 'catch' < TResult = never > ( onRejected ) { return this . then ( VOID0 , onRejected ) ; } param onRejected returns { PromiseBase < TResult > } catchAllowFatal < TResult = never > ( onRejected ) { return this . thenAllowFatal ( VOID0 , onRejected ) ; } param fin returns { PromiseBase < TResult > } 'finally' < TResult = never > ( fin ) { return this . then ( fin , fin ) ; } param fin returns { PromiseBase < TResult > } finallyAllowFatal < TResult = never > ( fin ) { return this . thenAllowFatal ( fin , fin ) ; } param fin param synchronous returns { PromiseBase } finallyThis ( fin , synchronous ? ) { const f = synchronous ? fin : ( ) => deferImmediate ( fin ) ; this . doneNow ( f , f ) ; return this ; } } export abstract class Resolvable < T > extends PromiseBase < T > { doneNow ( onFulfilled , onRejected ? ) { this . throwIfDisposed ( ) ; switch ( this . state ) { case TSDNPromise . State . Fulfilled : if ( onFulfilled ) onFulfilled ( this . _result ! ) ; break ; case TSDNPromise . State . Rejected : if ( onRejected ) onRejected ( this . _error ) ; break ; } } thenSynchronous < TFulfilled = T , TRejected = never > ( onFulfilled , onRejected ? ) { this . throwIfDisposed ( ) ; try { switch ( this . state ) { case TSDNPromise . State . Fulfilled : return onFulfilled ? resolve ( this . _result , onFulfilled , TSDNPromise . resolve ) : < any > this ; case TSDNPromise . State . Rejected : return onRejected ? resolve ( this . _error , onRejected , TSDNPromise . resolve ) : < any > this ; } } catch ( ex ) { return new Rejected < any > ( ex ) ; } throw new Error ( "Invalid state for a resolved promise." ) ; } } /**
 * The simplest usable version of a promise which returns synchronously the resolved state provided.
 */ export abstract class Resolved < T > extends Resolvable < T > { protected constructor ( state , result , error ? ) { super ( ) ; this . _result = result ; this . _error = error ; this . _state = state ; } } /**
 * A fulfilled Resolved<T>.  Provided for readability.
 */ export class Fulfilled < T > extends Resolved < T > { constructor ( value ) { super ( TSDNPromise . State . Fulfilled , value ) ; } } /**
 * A rejected Resolved<T>.  Provided for readability.
 */ export class Rejected < T > extends Resolved < T > { constructor ( error ) { super ( TSDNPromise . State . Rejected , VOID0 , error ) ; } } /**
 * Provided as a means for extending the interface of other PromiseLike<T> objects.
 */ class PromiseWrapper < T > extends Resolvable < T > { constructor ( private _target ) { super ( ) ; if ( ! _target ) throw new ArgumentNullException ( TARGET ) ; if ( ! isPromise ( _target ) ) throw new ArgumentException ( TARGET , "Must be a promise-like object." ) ; _target . then ( ( v ) => { this . _state = TSDNPromise . State . Fulfilled ; this . _result = v ; this . _error = VOID0 ; this . _target = VOID0 ; } , e => { this . _state = TSDNPromise . State . Rejected ; this . _error = e ; this . _target = VOID0 ; } ) ; } thenSynchronous < TFulfilled = T , TRejected = never > ( onFulfilled , onRejected ? ) { this . throwIfDisposed ( ) ; let t = this . _target ; if ( ! t ) return super . thenSynchronous ( onFulfilled , onRejected ) ; return new TSDNPromise < TFulfilled | TRejected > ( ( resolve , reject ) => { handleDispatch ( t , result => handleResolutionMethods ( resolve , reject , result , onFulfilled ) , error => onRejected ? handleResolutionMethods ( resolve , null , error , onRejected ) : reject ( error ) ) ; } , true ) ; } doneNow ( onFulfilled , onRejected ? ) { this . throwIfDisposed ( ) ; let t = this . _target ; if ( t ) handleDispatch ( t , onFulfilled , onRejected ) ; else super . doneNow ( onFulfilled , onRejected ) ; } protected _onDispose ( ) { super . _onDispose ( ) ; this . _target = VOID0 ; } } /**
 * This promise class that facilitates pending resolution.
 */ export class TSDNPromise < T > extends Resolvable < T > { private _waiting : IPromiseCallbacks < any > [ ] | null | undefined ; constructor ( resolver ? , forceSynchronous = false ) { super ( ) ; if ( resolver ) this . resolveUsing ( resolver , forceSynchronous ) ; } thenSynchronous < TFulfilled = T , TRejected = never > ( onFulfilled , onRejected ? ) { this . throwIfDisposed ( ) ; if ( this . _state ) return super . thenSynchronous ( onFulfilled , onRejected ) ; const p = new TSDNPromise < TFulfilled | TRejected > ( ) ; ( this . _waiting || ( this . _waiting = [ ] ) ) . push ( pools . PromiseCallbacks . init ( onFulfilled , onRejected , p ) ) ; return p ; } doneNow ( onFulfilled , onRejected ? ) { this . throwIfDisposed ( ) ; if ( this . _state ) return super . doneNow ( onFulfilled , onRejected ) ; ( this . _waiting || ( this . _waiting = [ ] ) ) . push ( pools . PromiseCallbacks . init ( onFulfilled , onRejected ) ) ; } protected _onDispose ( ) { super . _onDispose ( ) ; this . _resolvedCalled = VOID0 ; } protected _resolvedCalled : boolean | undefined ; resolveUsing ( resolver , forceSynchronous = false ) { if ( ! resolver ) throw new ArgumentNullException ( "resolver" ) ; if ( this . _resolvedCalled ) throw new InvalidOperationException ( ".resolve() already called." ) ; if ( this . state ) throw new InvalidOperationException ( "Already resolved: " + TSDNPromise . State [ this . state ] ) ; this . _resolvedCalled = true ; let state = 0 ; const rejectHandler = ( reason ) => { if ( state ) { console . warn ( state == - 1 ? "Rejection called multiple times" : "Rejection called after fulfilled." ) ; } else { state = - 1 ; this . _resolvedCalled = false ; this . reject ( reason ) ; } } ; const fulfillHandler = ( v ) => { if ( state ) { console . warn ( state == 1 ? "Fulfill called multiple times" : "Fulfill called after rejection." ) ; } else { state = 1 ; this . _resolvedCalled = false ; this . resolve ( v ) ; } } ; if ( forceSynchronous ) resolver ( fulfillHandler , rejectHandler ) ; else deferImmediate ( ( ) => resolver ( fulfillHandler , rejectHandler ) ) ; } private _emitDisposalRejection ( p ) { const d = p . wasDisposed ; if ( d ) this . _rejectInternal ( newODE ( ) ) ; return d ; } private _resolveInternal ( result ? ) { if ( this . wasDisposed ) return ; while ( result instanceof PromiseBase ) { let r = < any > result ; if ( this . _emitDisposalRejection ( r ) ) return ; switch ( r . state ) { case TSDNPromise . State . Pending : r . doneNow ( v => this . _resolveInternal ( v ) , e => this . _rejectInternal ( e ) ) ; return ; case TSDNPromise . State . Rejected : this . _rejectInternal ( r . error ) ; return ; case TSDNPromise . State . Fulfilled : result = r . result ; break ; } } if ( isPromise ( result ) ) { result . then ( v => this . _resolveInternal ( v ) , e => this . _rejectInternal ( e ) ) ; } else { this . _state = TSDNPromise . State . Fulfilled ; this . _result = result ; this . _error = VOID0 ; const o = this . _waiting ; if ( o ) { this . _waiting = VOID0 ; for ( let c of o ) { let { onFulfilled , promise } = c ; pools . PromiseCallbacks . recycle ( c ) ; handleResolution ( < any > promise , result , onFulfilled ) ; } o . length = 0 ; } } } private _rejectInternal ( error ) { if ( this . wasDisposed ) return ; this . _state = TSDNPromise . State . Rejected ; this . _error = error ; const o = this . _waiting ; if ( o ) { this . _waiting = null ; for ( let c of o ) { let { onRejected , promise } = c ; pools . PromiseCallbacks . recycle ( c ) ; if ( onRejected ) { handleResolution ( promise , error , onRejected ) ; } else if ( promise ) { promise . reject ( error ) ; } } o . length = 0 ; } } resolve ( result ? , throwIfSettled = false ) { this . throwIfDisposed ( ) ; if ( < any > result == this ) throw new InvalidOperationException ( "Cannot resolve a promise as itself." ) ; if ( this . _state ) { if ( ! throwIfSettled || this . _state == TSDNPromise . State . Fulfilled && this . _result === result ) return ; throw new InvalidOperationException ( "Changing the fulfilled state/value of a promise is not supported." ) ; } if ( this . _resolvedCalled ) { if ( throwIfSettled ) throw new InvalidOperationException ( ".resolve() already called." ) ; return ; } this . _resolveInternal ( result ) ; } reject ( error , throwIfSettled = false ) { this . throwIfDisposed ( ) ; if ( this . _state ) { if ( ! throwIfSettled || this . _state == TSDNPromise . State . Rejected && this . _error === error ) return ; throw new InvalidOperationException ( "Changing the rejected state/value of a promise is not supported." ) ; } if ( this . _resolvedCalled ) { if ( throwIfSettled ) throw new InvalidOperationException ( ".resolve() already called." ) ; return ; } this . _rejectInternal ( error ) ; } } /**
 * By providing an ArrayPromise we expose useful methods/shortcuts for dealing with array results.
 */ export class ArrayPromise < T > extends TSDNPromise < T [ ] > { param transform returns { PromiseBase < Array < any > > } map < U > ( transform ) { this . throwIfDisposed ( ) ; return new ArrayPromise < U > ( resolve => { this . doneNow ( result => resolve ( result . map ( transform ) ) ) ; } , true ) ; } reduce ( reduction , initialValue ? ) ; reduce < U > ( reduction , initialValue ) ; param reduction param initialValue returns { PromiseBase < any > } reduce < U > ( reduction , initialValue ? ) { return this . thenSynchronous ( result => result . reduce ( reduction , < any > initialValue ) ) ; } static fulfilled < T > ( value ) { return new ArrayPromise < T > ( resolve => value , true ) ; } } const PROMISE_COLLECTION = "PromiseCollection" ; /**
 * A Promise collection exposes useful methods for handling a collection of promises and their results.
 */ export class PromiseCollection < T > extends DisposableBase { private readonly _source : PromiseLike < T > [ ] ; constructor ( source ) { super ( PROMISE_COLLECTION ) ; this . _source = source && source . slice ( ) || [ ] ; } protected _onDispose ( ) { super . _onDispose ( ) ; this . _source . length = 0 ; ( < any > this ) . _source = null ; } returns { PromiseLike < PromiseLike < any > > [ ] } get promises ( ) : PromiseLike < T > [ ] { this . throwIfDisposed ( ) ; return this . _source . slice ( ) ; } returns { PromiseBase < any > } all ( ) { this . throwIfDisposed ( ) ; return TSDNPromise . all ( this . _source ) ; } returns { PromiseBase < any > } race ( ) { this . throwIfDisposed ( ) ; return TSDNPromise . race ( this . _source ) ; } returns { PromiseBase < PromiseLike < any > [ ] > } waitAll ( ) { this . throwIfDisposed ( ) ; return TSDNPromise . waitAll ( this . _source ) ; } param transform returns { PromiseBase < Array < any > > } map < U > ( transform ) { this . throwIfDisposed ( ) ; return new ArrayPromise < U > ( resolve => { this . all ( ) . doneNow ( result => resolve ( result . map ( transform ) ) ) ; } , true ) ; } param transform returns { PromiseCollection < U > } pipe < U > ( transform ) { this . throwIfDisposed ( ) ; return new PromiseCollection < U > ( this . _source . map ( p => handleSyncIfPossible ( p , transform ) ) ) ; } reduce ( reduction , initialValue ? ) ; reduce < U > ( reduction , initialValue ) ; param reduction param initialValue returns { PromiseBase < PromiseLike < any > > } reduce < U > ( reduction , initialValue ? ) { this . throwIfDisposed ( ) ; return TSDNPromise . wrap < U > ( this . _source . reduce ( ( previous , current , i , array ) => handleSyncIfPossible ( previous , ( p ) => handleSyncIfPossible ( current , ( c ) => reduction ( p , c , i , array ) ) ) , isPromise ( initialValue ) ? initialValue : new Fulfilled ( < any > initialValue ) ) ) ; } } module pools { export module PromiseCallbacks { let pool ; function getPool ( ) { return pool || ( pool = new ObjectPool < IPromiseCallbacks < any > > ( factory , c => { c . onFulfilled = NULL ; c . onRejected = NULL ; c . promise = NULL ; } , 40 ) ) ; } function factory ( ) { return { onFulfilled : NULL , onRejected : NULL , promise : NULL } ; } export function init < T > ( onFulfilled , onRejected ? , promise ? ) { const c = getPool ( ) . take ( ) ; c . onFulfilled = onFulfilled || undefined ; c . onRejected = onRejected || undefined ; c . promise = promise ; return c ; } export function recycle < T > ( c ) { getPool ( ) . give ( c ) ; } } } export module TSDNPromise { /**
     * The state of a promise.
     * https://github.com/domenic/promises-unwrapping/blob/master/docs/states-and-fates.md
     * If a promise is disposed the value will be undefined which will also evaluate (promise.state)==false.
     */ export enum State { Pending = 0 , Fulfilled = 1 , Rejected = - 1 } Object . freeze ( State ) ; export type Resolution < TResult > = TResult | PromiseLike < TResult > ; export interface Fulfill < T , TResult > { ( value ) : Resolution < TResult > ; } export interface Reject < TResult > { ( reason ) : Resolution < TResult > ; } export interface Then < T , TResult > { ( onfulfilled ? , onrejected ? ) : PromiseLike < TResult > ; ( onfulfilled ? , onrejected ? ) : PromiseLike < TResult > ; } export interface Executor < T > { ( resolve , reject ) : void ; } export interface Factory { < T > ( executor ) : PromiseLike < T > ; } export function factory < T > ( e ) { return new TSDNPromise ( e ) ; } param promises export function group < T > ( promises ) ; export function group < T > ( promise , ... rest ) ; export function group ( first , ... rest ) { if ( ! first && ! rest . length ) throw new ArgumentNullException ( "promises" ) ; return new PromiseCollection ( ( ( first ) instanceof ( Array ) ? first : [ first ] ) . concat ( rest ) ) ; } /**
     * Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected.
     */ export function all < T > ( promises ) ; export function all < T > ( promise , ... rest ) ; export function all ( first , ... rest ) { if ( ! first && ! rest . length ) throw new ArgumentNullException ( "promises" ) ; let promises = ( ( first ) instanceof ( Array ) ? first : [ first ] ) . concat ( rest ) ; if ( ! promises . length || promises . every ( v => ! v ) ) return new ArrayPromise < any > ( r => r ( promises ) , true ) ; return new ArrayPromise < any > ( ( resolve , reject ) => { let result = [ ] ; let len = promises . length ; result . length = len ; let remaining = new Set ( promises . map ( ( v , i ) => i ) ) ; let cleanup = ( ) => { reject = VOID0 ; resolve = VOID0 ; promises . length = 0 ; promises = VOID0 ; remaining . dispose ( ) ; remaining = VOID0 ; } ; let checkIfShouldResolve = ( ) => { let r = resolve ; if ( r && ! remaining . count ) { cleanup ( ) ; r ( result ) ; } } ; let onFulfill = ( v , i ) => { if ( resolve != null ) { result [ i ] = v ; remaining . remove ( i ) ; checkIfShouldResolve ( ) ; } } ; let onReject = ( e ? ) => { let r = reject ; if ( r ) { cleanup ( ) ; r ( e ) ; } } ; for ( let i = 0 ; remaining && i < len ; i ++ ) { let p = promises [ i ] ; if ( p ) p . then ( v => onFulfill ( v , i ) , onReject ) ; else remaining . remove ( i ) ; checkIfShouldResolve ( ) ; } } ) ; } /**
     * Returns a promise that is fulfilled with array of provided promises when all provided promises have resolved (fulfill or reject).
     * Unlike .all this method waits for all rejections as well as fulfillment.
     */ export function waitAll < T > ( promises ) ; export function waitAll < T > ( promise , ... rest ) ; export function waitAll ( first , ... rest ) { if ( ! first && ! rest . length ) throw new ArgumentNullException ( "promises" ) ; const promises = ( ( first ) instanceof ( Array ) ? first : [ first ] ) . concat ( rest ) ; if ( ! promises . length || promises . every ( v => ! v ) ) return new ArrayPromise < any > ( r => r ( promises ) , true ) ; return new ArrayPromise < any > ( ( resolve , reject ) => { let len = promises . length ; let remaining = new Set ( promises . map ( ( v , i ) => i ) ) ; let cleanup = ( ) => { reject = NULL ; resolve = NULL ; remaining . dispose ( ) ; remaining = NULL ; } ; let checkIfShouldResolve = ( ) => { let r = resolve ; if ( r && ! remaining . count ) { cleanup ( ) ; r ( promises ) ; } } ; let onResolved = ( i ) => { if ( remaining ) { remaining . remove ( i ) ; checkIfShouldResolve ( ) ; } } ; for ( let i = 0 ; remaining && i < len ; i ++ ) { let p = promises [ i ] ; if ( p ) p . then ( v => onResolved ( i ) , e => onResolved ( i ) ) ; else onResolved ( i ) ; } } ) ; } param promises returns export function race < T > ( promises ) ; export function race < T > ( promise , ... rest ) ; export function race ( first , ... rest ) { let promises = first && ( ( first ) instanceof ( Array ) ? first : [ first ] ) . concat ( rest ) ; if ( ! promises || ! promises . length || ! ( promises = promises . filter ( v => v != null ) ) . length ) throw new ArgumentException ( "Nothing to wait for." ) ; const len = promises . length ; if ( len == 1 ) return wrap ( promises [ 0 ] ) ; for ( let i = 0 ; i < len ; i ++ ) { const p = promises [ i ] ; if ( p instanceof PromiseBase && p . isSettled ) return p ; } return new TSDNPromise ( ( resolve , reject ) => { let cleanup = ( ) => { reject = NULL ; resolve = NULL ; promises . length = 0 ; promises = NULL ; } ; let onResolve = ( r , v ) => { if ( r ) { cleanup ( ) ; r ( v ) ; } } ; let onFulfill = ( v ) => onResolve ( resolve , v ) ; let onReject = ( e ? ) => onResolve ( reject , e ) ; for ( let p of promises ) { if ( ! resolve ) break ; p . then ( onFulfill , onReject ) ; } } ) ; } returns export function resolve ( ) ; param value returns export function resolve < T > ( value ) ; export function resolve ( value ? ) { return isPromise ( value ) ? wrap ( value ) : new Fulfilled ( value ) ; } param resolver param forceSynchronous returns { TSDNPromise } export function using < T > ( resolver , forceSynchronous = false ) { return new TSDNPromise < T > ( resolver , forceSynchronous ) ; } param resolutions export function resolveAll < T > ( resolutions ) ; export function resolveAll < T > ( promise , ... rest ) ; export function resolveAll ( first , ... rest ) { if ( ! first && ! rest . length ) throw new ArgumentNullException ( "resolutions" ) ; return new PromiseCollection ( ( ( first ) instanceof ( Array ) ? first : [ first ] ) . concat ( rest ) . map ( ( v ) => resolve ( v ) ) ) ; } param source param transform returns { PromiseCollection < T > } export function map < T , U > ( source , transform ) { return new PromiseCollection < U > ( source . map ( d => new TSDNPromise < U > ( ( r , j ) => { try { r ( transform ( d ) ) ; } catch ( ex ) { j ( ex ) ; } } ) ) ) ; } param reason returns export function reject < T > ( reason ) { return new Rejected < T > ( reason ) ; } param target returns export function wrap < T > ( target ) { if ( ! target ) throw new ArgumentNullException ( TARGET ) ; return isPromise ( target ) ? ( target instanceof PromiseBase ? target : new PromiseWrapper ( target ) ) : new Fulfilled < T > ( target ) ; } param then returns { PromiseWrapper < T > } export function createFrom < T > ( then ) { if ( ! then ) throw new ArgumentNullException ( THEN ) ; return new PromiseWrapper < T > ( { then : then } ) ; } } interface IPromiseCallbacks < T > { onFulfilled ? : TSDNPromise . Fulfill < T , any > ; onRejected ? : TSDNPromise . Reject < any > ; promise ? : TSDNPromise < any > ; } export { TSDNPromise as Promise } ; export default TSDNPromise ;