Path: blob/trunk/third_party/closure/goog/promise/thenable.js
2868 views
// Copyright 2013 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314goog.provide('goog.Thenable');1516/** @suppress {extraRequire} */17goog.forwardDeclare('goog.Promise'); // for the type reference.18192021/**22* Provides a more strict interface for Thenables in terms of23* http://promisesaplus.com for interop with {@see goog.Promise}.24*25* @interface26* @extends {IThenable<TYPE>}27* @template TYPE28*/29goog.Thenable = function() {};303132/**33* Adds callbacks that will operate on the result of the Thenable, returning a34* new child Promise.35*36* If the Thenable is fulfilled, the {@code onFulfilled} callback will be37* invoked with the fulfillment value as argument, and the child Promise will38* be fulfilled with the return value of the callback. If the callback throws39* an exception, the child Promise will be rejected with the thrown value40* instead.41*42* If the Thenable is rejected, the {@code onRejected} callback will be invoked43* with the rejection reason as argument, and the child Promise will be rejected44* with the return value of the callback or thrown value.45*46* @param {?(function(this:THIS, TYPE): VALUE)=} opt_onFulfilled A47* function that will be invoked with the fulfillment value if the Promise48* is fulfilled.49* @param {?(function(this:THIS, *): *)=} opt_onRejected A function that will50* be invoked with the rejection reason if the Promise is rejected.51* @param {THIS=} opt_context An optional context object that will be the52* execution context for the callbacks. By default, functions are executed53* with the default this.54*55* @return {RESULT} A new Promise that will receive the result56* of the fulfillment or rejection callback.57* @template VALUE58* @template THIS59*60* When a Promise (or thenable) is returned from the fulfilled callback,61* the result is the payload of that promise, not the promise itself.62*63* @template RESULT := type('goog.Promise',64* cond(isUnknown(VALUE), unknown(),65* mapunion(VALUE, (V) =>66* cond(isTemplatized(V) && sub(rawTypeOf(V), 'IThenable'),67* templateTypeOf(V, 0),68* cond(sub(V, 'Thenable'),69* unknown(),70* V)))))71* =:72*73*/74goog.Thenable.prototype.then = function(75opt_onFulfilled, opt_onRejected, opt_context) {};767778/**79* An expando property to indicate that an object implements80* {@code goog.Thenable}.81*82* {@see addImplementation}.83*84* @const85*/86goog.Thenable.IMPLEMENTED_BY_PROP = '$goog_Thenable';878889/**90* Marks a given class (constructor) as an implementation of Thenable, so91* that we can query that fact at runtime. The class must have already92* implemented the interface.93* Exports a 'then' method on the constructor prototype, so that the objects94* also implement the extern {@see goog.Thenable} interface for interop with95* other Promise implementations.96* @param {function(new:goog.Thenable,...?)} ctor The class constructor. The97* corresponding class must have already implemented the interface.98*/99goog.Thenable.addImplementation = function(ctor) {100// Use bracket notation instead of goog.exportSymbol() so that the compiler101// won't create a 'var ctor;' extern when the "create externs from exports"102// mode is enabled.103ctor.prototype['then'] = ctor.prototype.then;104if (COMPILED) {105ctor.prototype[goog.Thenable.IMPLEMENTED_BY_PROP] = true;106} else {107// Avoids dictionary access in uncompiled mode.108ctor.prototype.$goog_Thenable = true;109}110};111112113/**114* @param {?} object115* @return {boolean} Whether a given instance implements {@code goog.Thenable}.116* The class/superclass of the instance must call {@code addImplementation}.117*/118goog.Thenable.isImplementedBy = function(object) {119if (!object) {120return false;121}122try {123if (COMPILED) {124return !!object[goog.Thenable.IMPLEMENTED_BY_PROP];125}126return !!object.$goog_Thenable;127} catch (e) {128// Property access seems to be forbidden.129return false;130}131};132133134