WinJS.Promise.Promise constructor
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.
Syntax
var promise = new WinJS.Promise.Promise(init, onCancel);
Parameters
init
Type: FunctionThe 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: The init code should invoke this when the operation is complete, passing the operation's results as an argument.
- errorDispatch: The init code should invoke this function when an error occurs, which will place the promise into the error state. The argument to errorDispatch should be an object created with WinJS.Promise.ErrorFromName.
- progressDispatch: If the operation wishes to support progress, The init code should invoke this function periodically while the operation is underway, passing an intermediate result as the argument.
This parameter is optional.
onCancel
Type: FunctionThe function to call if a consumer of this promise wants to cancel its undone work. Promises are not required to support cancellation.
Requirements
Minimum WinJS version |
WinJS 1.0 |
Namespace |
WinJS.Promise |