Path: blob/trunk/third_party/closure/goog/promise/thenable.js
4206 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/5goog.module('goog.Thenable');6goog.module.declareLegacyNamespace();78/** @suppress {extraRequire} used in complex type */9const GoogPromise = goog.requireType('goog.Promise'); // for the type reference.1011/**12* Provides a more strict interface for Thenables in terms of13* http://promisesaplus.com for interop with {@see GoogPromise}.14*15* @interface16* @extends {IThenable<TYPE>}17* @template TYPE18*/19function Thenable() {}2021/**22* Adds callbacks that will operate on the result of the Thenable, returning a23* new child Promise.24*25* If the Thenable is fulfilled, the `onFulfilled` callback will be26* invoked with the fulfillment value as argument, and the child Promise will27* be fulfilled with the return value of the callback. If the callback throws28* an exception, the child Promise will be rejected with the thrown value29* instead.30*31* If the Thenable is rejected, the `onRejected` callback will be invoked with32* the rejection reason as argument. Similar to the fulfilled case, the child33* Promise will then be resolved with the return value of the callback, or34* rejected with the thrown value if the callback throws an exception.35*36* @param {?(function(this:THIS, TYPE): VALUE)=} opt_onFulfilled A37* function that will be invoked with the fulfillment value if the Promise38* is fulfilled.39* @param {?(function(this:THIS, *): *)=} opt_onRejected A function that will40* be invoked with the rejection reason if the Promise is rejected.41* @param {THIS=} opt_context An optional context object that will be the42* execution context for the callbacks. By default, functions are executed43* with the default this.44*45* @return {RESULT} A new Promise that will receive the result46* of the fulfillment or rejection callback.47* @template VALUE48* @template THIS49*50* When a Promise (or thenable) is returned from the fulfilled callback,51* the result is the payload of that promise, not the promise itself.52*53* @template RESULT := type('goog.Promise',54* cond(isUnknown(VALUE), unknown(),55* mapunion(VALUE, (V) =>56* cond(isTemplatized(V) && sub(rawTypeOf(V), 'IThenable'),57* templateTypeOf(V, 0),58* cond(sub(V, 'Thenable'),59* unknown(),60* V)))))61* =:62*63*/64Thenable.prototype.then = function(65opt_onFulfilled, opt_onRejected, opt_context) {};6667/**68* An expando property to indicate that an object implements69* `Thenable`.70*71* {@see addImplementation}.72*73* @const74*/75Thenable.IMPLEMENTED_BY_PROP = '$goog_Thenable';7677/**78* Marks a given class (constructor) as an implementation of Thenable, so79* that we can query that fact at runtime. The class must have already80* implemented the interface.81* Exports a 'then' method on the constructor prototype, so that the objects82* also implement the extern {@see Thenable} interface for interop with83* other Promise implementations.84* @param {function(new:Thenable,...?)} ctor The class constructor. The85* corresponding class must have already implemented the interface.86*/87Thenable.addImplementation = function(ctor) {88if (COMPILED) {89ctor.prototype[Thenable.IMPLEMENTED_BY_PROP] = true;90} else {91// Avoids dictionary access in uncompiled mode.92ctor.prototype.$goog_Thenable = true;93}94};9596/**97* @param {?} object98* @return {boolean} Whether a given instance implements `Thenable`.99* The class/superclass of the instance must call `addImplementation`.100*/101Thenable.isImplementedBy = function(object) {102if (!object) {103return false;104}105try {106if (COMPILED) {107return !!object[Thenable.IMPLEMENTED_BY_PROP];108}109return !!object.$goog_Thenable;110} catch (e) {111// Property access seems to be forbidden.112return false;113}114};115116exports = Thenable;117118119