Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/promise/thenable.js
4206 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
goog.module('goog.Thenable');
7
goog.module.declareLegacyNamespace();
8
9
/** @suppress {extraRequire} used in complex type */
10
const GoogPromise = goog.requireType('goog.Promise'); // for the type reference.
11
12
/**
13
* Provides a more strict interface for Thenables in terms of
14
* http://promisesaplus.com for interop with {@see GoogPromise}.
15
*
16
* @interface
17
* @extends {IThenable<TYPE>}
18
* @template TYPE
19
*/
20
function Thenable() {}
21
22
/**
23
* Adds callbacks that will operate on the result of the Thenable, returning a
24
* new child Promise.
25
*
26
* If the Thenable is fulfilled, the `onFulfilled` callback will be
27
* invoked with the fulfillment value as argument, and the child Promise will
28
* be fulfilled with the return value of the callback. If the callback throws
29
* an exception, the child Promise will be rejected with the thrown value
30
* instead.
31
*
32
* If the Thenable is rejected, the `onRejected` callback will be invoked with
33
* the rejection reason as argument. Similar to the fulfilled case, the child
34
* Promise will then be resolved with the return value of the callback, or
35
* rejected with the thrown value if the callback throws an exception.
36
*
37
* @param {?(function(this:THIS, TYPE): VALUE)=} opt_onFulfilled A
38
* function that will be invoked with the fulfillment value if the Promise
39
* is fulfilled.
40
* @param {?(function(this:THIS, *): *)=} opt_onRejected A function that will
41
* be invoked with the rejection reason if the Promise is rejected.
42
* @param {THIS=} opt_context An optional context object that will be the
43
* execution context for the callbacks. By default, functions are executed
44
* with the default this.
45
*
46
* @return {RESULT} A new Promise that will receive the result
47
* of the fulfillment or rejection callback.
48
* @template VALUE
49
* @template THIS
50
*
51
* When a Promise (or thenable) is returned from the fulfilled callback,
52
* the result is the payload of that promise, not the promise itself.
53
*
54
* @template RESULT := type('goog.Promise',
55
* cond(isUnknown(VALUE), unknown(),
56
* mapunion(VALUE, (V) =>
57
* cond(isTemplatized(V) && sub(rawTypeOf(V), 'IThenable'),
58
* templateTypeOf(V, 0),
59
* cond(sub(V, 'Thenable'),
60
* unknown(),
61
* V)))))
62
* =:
63
*
64
*/
65
Thenable.prototype.then = function(
66
opt_onFulfilled, opt_onRejected, opt_context) {};
67
68
/**
69
* An expando property to indicate that an object implements
70
* `Thenable`.
71
*
72
* {@see addImplementation}.
73
*
74
* @const
75
*/
76
Thenable.IMPLEMENTED_BY_PROP = '$goog_Thenable';
77
78
/**
79
* Marks a given class (constructor) as an implementation of Thenable, so
80
* that we can query that fact at runtime. The class must have already
81
* implemented the interface.
82
* Exports a 'then' method on the constructor prototype, so that the objects
83
* also implement the extern {@see Thenable} interface for interop with
84
* other Promise implementations.
85
* @param {function(new:Thenable,...?)} ctor The class constructor. The
86
* corresponding class must have already implemented the interface.
87
*/
88
Thenable.addImplementation = function(ctor) {
89
if (COMPILED) {
90
ctor.prototype[Thenable.IMPLEMENTED_BY_PROP] = true;
91
} else {
92
// Avoids dictionary access in uncompiled mode.
93
ctor.prototype.$goog_Thenable = true;
94
}
95
};
96
97
/**
98
* @param {?} object
99
* @return {boolean} Whether a given instance implements `Thenable`.
100
* The class/superclass of the instance must call `addImplementation`.
101
*/
102
Thenable.isImplementedBy = function(object) {
103
if (!object) {
104
return false;
105
}
106
try {
107
if (COMPILED) {
108
return !!object[Thenable.IMPLEMENTED_BY_PROP];
109
}
110
return !!object.$goog_Thenable;
111
} catch (e) {
112
// Property access seems to be forbidden.
113
return false;
114
}
115
};
116
117
exports = Thenable;
118
119