WinJS.Promise

WinJS.Promise 函数和源码:

 

 

supportedForProcessing: false


addEventListener: function Promise_addEventListener(eventType, listener, capture) { /// /// /// Adds an event listener to the control. /// /// /// The type (name) of the event. /// /// /// The listener to invoke when the event is raised. /// /// /// Specifies whether or not to initiate capture. /// /// promiseEventListeners.addEventListener(eventType, listener, capture); }


any: function Promise_any(values) { /// /// /// Returns a promise that is fulfilled when one of the input promises /// has been fulfilled. /// /// /// An array that contains promise objects or objects whose property /// values include promise objects. /// /// /// A promise that on fulfillment yields the value of the input (complete or error). /// /// return new Promise( function (complete, error, progress) { var keys = Object.keys(values); var errors = Array.isArray(values) ? [] : {}; if (keys.length === 0) { complete(); } var canceled = 0; keys.forEach(function (key) { Promise.as(values[key]).then( function () { complete({ key: key, value: values[key] }); }, function (e) { if (e instanceof Error && e.name === canceledName) { if ((++canceled) === keys.length) { complete(WinJS.Promise.cancel); } return; } error({ key: key, value: values[key] }); } ); }); }, function () { var keys = Object.keys(values); keys.forEach(function (key) { var promise = Promise.as(values[key]); if (typeof promise.cancel === "function") { promise.cancel(); } }); } ); }


as: function Promise_as(value) { /// /// /// Returns a promise. If the object is already a promise it is returned; /// otherwise the object is wrapped in a promise. /// /// /// The value to be treated as a promise. /// /// /// A promise. /// /// if (value && typeof value === "object" && typeof value.then === "function") { return value; } return new CompletePromise(value); }


dispatchEvent: function Promise_dispatchEvent(eventType, details) { /// /// /// Raises an event of the specified type and properties. /// /// /// The type (name) of the event. /// /// /// The set of additional properties to be attached to the event object. /// /// /// Specifies whether preventDefault was called on the event. /// /// return promiseEventListeners.dispatchEvent(eventType, details); }


is: function Promise_is(value) { /// /// /// Determines whether a value fulfills the promise contract. /// /// /// A value that may be a promise. /// /// /// true if the specified value is a promise, otherwise false. /// /// return value && typeof value === "object" && typeof value.then === "function"; }


join: function Promise_join(values) { /// /// /// Creates a promise that is fulfilled when all the values are fulfilled. /// /// /// An object whose fields contain values, some of which may be promises. /// /// /// 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. /// /// return new Promise( function (complete, error, progress) { var keys = Object.keys(values); var errors = Array.isArray(values) ? [] : {}; var results = Array.isArray(values) ? [] : {}; var undefineds = 0; var pending = keys.length; var argDone = function (key) { if ((--pending) === 0) { var errorCount = Object.keys(errors).length; if (errorCount === 0) { complete(results); } else { var canceledCount = 0; keys.forEach(function (key) { var e = errors[key]; if (e instanceof Error && e.name === canceledName) { canceledCount++; } }); if (canceledCount === errorCount) { complete(WinJS.Promise.cancel); } else { error(errors); } } } else { progress({ Key: key, Done: true }); } }; keys.forEach(function (key) { var value = values[key]; if (value === undefined) { undefineds++; } else { Promise.then(value, function (value) { results[key] = value; argDone(key); }, function (value) { errors[key] = value; argDone(key); } ); } }); pending -= undefineds; if (pending === 0) { complete(results); return; } }, function () { Object.keys(values).forEach(function (key) { var promise = Promise.as(values[key]); if (typeof promise.cancel === "function") { promise.cancel(); } }); } ); }


removeEventListener: function Promise_removeEventListener(eventType, listener, capture) { /// /// /// Removes an event listener from the control. /// /// /// The type (name) of the event. /// /// /// The listener to remove. /// /// /// Specifies whether or not to initiate capture. /// /// promiseEventListeners.removeEventListener(eventType, listener, capture); }


then: function Promise_then(value, onComplete, onError, onProgress) { /// /// /// A static version of the promise instance method then(). /// /// /// the value to be treated as a promise. /// /// /// The function to be called if the promise is fulfilled with a value. /// If it is null, the promise simply /// returns the value. The value is passed as the single argument. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. /// /// /// 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. /// /// /// A promise whose value is the result of executing the provided complete function. /// /// return Promise.as(value).then(onComplete, onError, onProgress); }


thenEach: function Promise_thenEach(values, onComplete, onError, onProgress) { /// /// /// 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. /// /// /// A set of values (which could be either an array or an object) of which some or all are promises. /// /// /// The function to be called if the promise is fulfilled with a value. /// If the value is null, the promise returns the value. /// The value is passed as the single argument. /// /// /// The function to be called if the promise is fulfilled with an error. The error /// is passed as the single argument. /// /// /// 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. /// /// /// A promise that is the result of calling Promise.join on the values parameter. /// /// var result = Array.isArray(values) ? [] : {}; Object.keys(values).forEach(function (key) { result[key] = Promise.as(values[key]).then(onComplete, onError, onProgress); }); return Promise.join(result); }


timeout: function Promise_timeout(time, promise) { /// /// /// Creates a promise that is fulfilled after a timeout. /// /// /// The timeout period in milliseconds. If this value is zero or not specified /// setImmediate is called, otherwise setTimeout is called. /// /// /// A promise that will be canceled if it doesn't complete before the /// timeout has expired. /// /// /// A promise that is completed asynchronously after the specified timeout. /// /// var to = timeout(time); return promise ? timeoutWithPromise(to, promise) : to; }


wrap: function Promise_wrap(value) { /// /// /// Wraps a non-promise value in a promise. You can use this function if you need /// to pass a value to a function that requires a promise. /// /// /// Some non-promise value to be wrapped in a promise. /// /// /// A promise that is successfully fulfilled with the specified value /// /// return new CompletePromise(value); }


wrapError: function Promise_wrapError(error) { /// /// /// 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. /// /// /// A non-promise error value to be wrapped in a promise. /// /// /// A promise that is in an error state with the specified value. /// /// return new ErrorPromise(error); }


cancel: [object Object]


onerror: undefined


posted @ 2012-12-29 23:28  Angkor--:--  阅读(413)  评论(0编辑  收藏  举报