Class Promise

Provides a mechanism to schedule work to be done on a value that has not yet been computed. It is an abstraction for managing interactions with asynchronous APIs. For more information about asynchronous programming, see Asynchronous programming. For more information about promises in JavaScript, see Asynchronous programming in JavaScript. For more information about using promises, see the WinJS Promise sample.

Index

Constructor methods

Methods

Constructor methods

constructor(init?: (completeDispatch: any, errorDispatch: any, progressDispatch: any) => void, onCancel?: Function): Promise

A promise provides a mechanism to schedule work to be done on a value that has not yet been computed. It is a convenient abstraction for managing interactions with asynchronous APIs. For more information about asynchronous programming, see Asynchronous programming. For more information about promises in JavaScript, see Asynchronous programming in JavaScript. For more information about using promises, see the WinJS Promise sample.

constructor

Parameters

  • init?: (completeDispatch: any, errorDispatch: any, progressDispatch: any) => void optional

    The function that is called during construction of the Promise that contains the implementation of the operation that the Promise will represent. This can be synchronous or asynchronous, depending on the nature of the operation. Note that placing code within this function does not automatically run it asynchronously; that must be done explicitly with other asynchronous APIs such as setImmediate, setTimeout, requestAnimationFrame, and the Windows Runtime asynchronous APIs. The init function is given three arguments: completeDispatch, errorDispatch, progressDispatch. This parameter is optional.

  • onCancel?: Function optional

    The function to call if a consumer of this promise wants to cancel its undone work. Promises are not required to support cancellation.

Returns

Promise

Methods

public addEventListener(type: string, listener: Function, capture?: boolean)

Adds an event listener for the promise.

Parameters

  • type: string

    The type (name) of the event.

  • listener: Function

    The listener to invoke when the event is raised.

  • capture?: boolean optional

    true to initiate capture, otherwise false.

public static any(value: Promise[]): Promise

Returns a promise that is fulfilled when one of the input promises has been fulfilled.

Parameters

  • value: Promise[]

    An array that contains Promise objects or objects whose property values include Promise objects.

Returns

Promise

A promise that on fulfillment yields the value of the input (complete or error).

public static as(value: U): Promise

Returns a promise. If the object is already a Promise it is returned; otherwise the object is wrapped in a Promise. You can use this function when you need to treat a non-Promise object like a Promise, for example when you are calling a function that expects a promise, but already have the value needed rather than needing to get it asynchronously.

Parameters

  • value: U

    The value to be treated as a Promise.

Returns

Promise

The promise.

public cancel()

Attempts to cancel the fulfillment of a promised value. If the promise hasn't already been fulfilled and cancellation is supported, the promise enters the error state with a value of Error("Canceled").

public dispatchEvent(type: string, details: any): boolean

Raises an event of the specified type and properties.

Parameters

  • type: string

    The type (name) of the event.

  • details: any

    The set of additional properties to be attached to the event object.

Returns

boolean

true if preventDefault was called on the event; otherwise, false.

public done(onComplete?: (value: T) => any, onError?: (error: any) => any, onProgress?: (progress: any) => void)

Allows you to specify the work to be done on the fulfillment of the promised value, the error handling to be performed if the promise fails to fulfill a value, and the handling of progress notifications along the way. After the handlers have finished executing, this function throws any error that would have been returned from then as a promise in the error state. For more information about the differences between then and done, see the following topics: Quickstart: using promises in JavaScript How to handle errors when using promises in JavaScript Chaining promises in JavaScript.

Parameters

  • onComplete?: (value: T) => any optional

    The function to be called if the promise is fulfilled successfully with a value. The fulfilled value is passed as the single argument. If the value is null, the fulfilled value is returned. The value returned from the function becomes the fulfilled value of the promise returned by then. If an exception is thrown while executing the function, the promise returned by then moves into the error state.

  • onError?: (error: any) => any optional

    The function to be called if the promise is fulfilled with an error. The error is passed as the single argument. If it is null, the error is forwarded. The value returned from the function is the fulfilled value of the promise returned by then.

  • onProgress?: (progress: any) => void optional

    The function to be called if the promise reports progress. Data about the progress is passed as the single argument. Promises are not required to support progress.

public static is(value: any): boolean

Determines whether a value fulfills the promise contract.

Parameters

  • value: any

    A value that may be a promise.

Returns

boolean

true if the object conforms to the promise contract (has a then function), otherwise false.

public static join(values: any): Promise

Creates a Promise that is fulfilled when all the values are fulfilled.

Parameters

  • values: any

    An object whose members contain values, some of which may be promises.

Returns

Promise

A Promise whose value is an object with the same field names as those of the object in the values parameter, where each field value is the fulfilled value of a promise.

public onerror(eventInfo: CustomEvent)

Occurs when there is an error in processing a promise.

Parameters

  • eventInfo: CustomEvent

    An object that contains information about the event.

public removeEventListener(eventType: string, listener: Function, capture?: boolean)

Removes an event listener from the control.

Parameters

  • eventType: string

    The type (name) of the event.

  • listener: Function

    The listener to remove.

  • capture?: boolean optional

    Specifies whether or not to initiate capture.

public then(onComplete?: (value: T) => WinJS.Promise<U>, onError?: (error: any) => WinJS.Promise<U>, onProgress?: (progress: any) => void): Promise

Allows you to specify the work to be done on the fulfillment of the promised value, the error handling to be performed if the promise fails to fulfill a value, and the handling of progress notifications along the way. For more information about the differences between then and done, see the following topics: Quickstart: using promises in JavaScript How to handle errors when using promises in JavaScript Chaining promises in JavaScript.

Parameters

  • onComplete?: (value: T) => WinJS.Promise<U> optional

    The function to be called if the promise is fulfilled successfully with a value. The value is passed as the single argument. If the value is null, the value is returned. The value returned from the function becomes the fulfilled value of the promise returned by then. If an exception is thrown while this function is being executed, the promise returned by then moves into the error state.

  • onError?: (error: any) => WinJS.Promise<U> optional

    The function to be called if the promise is fulfilled with an error. The error is passed as the single argument. In different cases this object may be of different types, so it is necessary to test the object for the properties you expect. If the error is null, it is forwarded. The value returned from the function becomes the value of the promise returned by the then function.

  • onProgress?: (progress: any) => void optional

    The function to be called if the promise reports progress. Data about the progress is passed as the single argument. Promises are not required to support progress.

Returns

Promise

The promise whose value is the result of executing the onComplete function.

public then(onComplete?: (value: T) => WinJS.Promise<U>, onError?: (error: any) => U, onProgress?: (progress: any) => void): Promise

Allows you to specify the work to be done on the fulfillment of the promised value, the error handling to be performed if the promise fails to fulfill a value, and the handling of progress notifications along the way. For more information about the differences between then and done, see the following topics: Quickstart: using promises in JavaScript How to handle errors when using promises in JavaScript Chaining promises in JavaScript.

Parameters

  • onComplete?: (value: T) => WinJS.Promise<U> optional

    The function to be called if the promise is fulfilled successfully with a value. The value is passed as the single argument. If the value is null, the value is returned. The value returned from the function becomes the fulfilled value of the promise returned by then. If an exception is thrown while this function is being executed, the promise returned by then moves into the error state.

  • onError?: (error: any) => U optional

    The function to be called if the promise is fulfilled with an error. The error is passed as the single argument. In different cases this object may be of different types, so it is necessary to test the object for the properties you expect. If the error is null, it is forwarded. The value returned from the function becomes the value of the promise returned by the then function.

  • onProgress?: (progress: any) => void optional

    The function to be called if the promise reports progress. Data about the progress is passed as the single argument. Promises are not required to support progress.

Returns

Promise

The promise whose value is the result of executing the onComplete function.

public then(onComplete?: (value: T) => U, onError?: (error: any) => WinJS.Promise<U>, onProgress?: (progress: any) => void): Promise

Allows you to specify the work to be done on the fulfillment of the promised value, the error handling to be performed if the promise fails to fulfill a value, and the handling of progress notifications along the way. For more information about the differences between then and done, see the following topics: Quickstart: using promises in JavaScript How to handle errors when using promises in JavaScript Chaining promises in JavaScript.

Parameters

  • onComplete?: (value: T) => U optional

    The function to be called if the promise is fulfilled successfully with a value. The value is passed as the single argument. If the value is null, the value is returned. The value returned from the function becomes the fulfilled value of the promise returned by then. If an exception is thrown while this function is being executed, the promise returned by then moves into the error state.

  • onError?: (error: any) => WinJS.Promise<U> optional

    The function to be called if the promise is fulfilled with an error. The error is passed as the single argument. In different cases this object may be of different types, so it is necessary to test the object for the properties you expect. If the error is null, it is forwarded. The value returned from the function becomes the value of the promise returned by the then function.

  • onProgress?: (progress: any) => void optional

    The function to be called if the promise reports progress. Data about the progress is passed as the single argument. Promises are not required to support progress.

Returns

Promise

The promise whose value is the result of executing the onComplete function.

public then(onComplete?: (value: T) => U, onError?: (error: any) => U, onProgress?: (progress: any) => void): Promise

Allows you to specify the work to be done on the fulfillment of the promised value, the error handling to be performed if the promise fails to fulfill a value, and the handling of progress notifications along the way. For more information about the differences between then and done, see the following topics: Quickstart: using promises in JavaScript How to handle errors when using promises in JavaScript Chaining promises in JavaScript.

Parameters

  • onComplete?: (value: T) => U optional

    The function to be called if the promise is fulfilled successfully with a value. The value is passed as the single argument. If the value is null, the value is returned. The value returned from the function becomes the fulfilled value of the promise returned by then. If an exception is thrown while this function is being executed, the promise returned by then moves into the error state.

  • onError?: (error: any) => U optional

    The function to be called if the promise is fulfilled with an error. The error is passed as the single argument. In different cases this object may be of different types, so it is necessary to test the object for the properties you expect. If the error is null, it is forwarded. The value returned from the function becomes the value of the promise returned by the then function.

  • onProgress?: (progress: any) => void optional

    The function to be called if the promise reports progress. Data about the progress is passed as the single argument. Promises are not required to support progress.

Returns

Promise

The promise whose value is the result of executing the onComplete function.

public static thenEach(values: any, complete?: (value: any) => void, error?: (error: any) => void, progress?: (progress: any) => void): Promise

Performs an operation on all the input promises and returns a promise that has the shape of the input and contains the result of the operation that has been performed on each input.

Parameters

  • values: any

    A set of values (which could be either an array or an object) of which some or all are promises..

  • complete?: (value: any) => void optional

    The function to be called if the promise is fulfilled with a value. This function takes a single argument, which is the fulfilled value of the promise.

  • error?: (error: any) => void optional

    The function to be called if the promise is fulfilled with an error. This function takes a single argument, which is the error value of the promise.

  • progress?: (progress: any) => void optional

    The function to be called if the promise reports progress. This function takes a single argument, which is the data about the progress of the promise. Promises are not required to support progress.

Returns

Promise

A Promise that is the result of calling join on the values parameter.

public static timeout(timeout: number, promise?: Promise): Promise

This method has two forms: WinJS.Promise.timeout(timeout) and WinJS.Promise.timeout(timeout, promise). WinJS.Promise.timeout(timeout) creates a promise that is completed asynchronously after the specified timeout, essentially wrapping a call to setTimeout within a promise. WinJS.Promise.timeout(timeout, promise) sets a timeout period for completion of the specified promise, automatically canceling the promise if it is not completed within the timeout period.

Parameters

  • timeout: number

    The timeout period in milliseconds. If this value is zero or not specified, msSetImmediate is called, otherwise setTimeout is called.

  • promise?: Promise optional

    Optional. A promise that will be canceled if it doesn't complete within the timeout period.

Returns

Promise

If the promise parameter is omitted, returns a promise that will be fulfilled after the timeout period. If the promise paramater is provided, the same promise is returned.

public static wrap(value: U): Promise

Wraps a non-promise value in a promise. This method is like wrapError, which allows you to produce a Promise in error conditions, in that it allows you to return a Promise in success conditions.

Parameters

  • value: U

    Some non-promise value to be wrapped in a promise.

Returns

Promise

A promise that is successfully fulfilled with the specified value.

public static wrapError(error: U): Promise

Wraps a non-promise error value in a promise. You can use this function if you need to pass an error to a function that requires a promise.

Parameters

  • error: U

    A non-promise error value to be wrapped in a promise.

Returns

Promise

A promise that is in an error state with the specified value.