| 1 |
// Copyright 2012 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Defines an interface that represents a Result. |
| 17 |
* |
| 18 |
* NOTE: goog.result is soft deprecated - we expect to replace this and |
| 19 |
* {@link goog.async.Deferred} with {@link goog.Promise}. |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.result.Result'); |
| 23 |
|
| 24 |
goog.require('goog.Thenable'); |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* A Result object represents a value returned by an asynchronous |
| 30 |
* operation at some point in the future (e.g. a network fetch). This is akin |
| 31 |
* to a 'Promise' or a 'Future' in other languages and frameworks. |
| 32 |
* |
| 33 |
* @interface |
| 34 |
* @extends {goog.Thenable} |
| 35 |
* @deprecated Use {@link goog.Promise} instead - http://go/promisemigration |
| 36 |
*/ |
| 37 |
goog.result.Result = function() {}; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Attaches handlers to be called when the value of this Result is available. |
| 42 |
* Handlers are called in the order they were added by wait. |
| 43 |
* |
| 44 |
* @param {function(this:T, !goog.result.Result)} handler The function called |
| 45 |
* when the value is available. The function is passed the Result object as |
| 46 |
* the only argument. |
| 47 |
* @param {T=} opt_scope Optional scope for the handler. |
| 48 |
* @template T |
| 49 |
*/ |
| 50 |
goog.result.Result.prototype.wait = function(handler, opt_scope) {}; |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* The States this object can be in. |
| 55 |
* |
| 56 |
* @enum {string} |
| 57 |
* @deprecated Use {@link goog.Promise} instead - http://go/promisemigration |
| 58 |
*/ |
| 59 |
goog.result.Result.State = { |
| 60 |
/** The operation was a success and the value is available. */ |
| 61 |
SUCCESS: 'success', |
| 62 |
|
| 63 |
/** The operation resulted in an error. */ |
| 64 |
ERROR: 'error', |
| 65 |
|
| 66 |
/** The operation is incomplete and the value is not yet available. */ |
| 67 |
PENDING: 'pending' |
| 68 |
}; |
| 69 |
|
| 70 |
|
| 71 |
/** |
| 72 |
* @return {!goog.result.Result.State} The state of this Result. |
| 73 |
*/ |
| 74 |
goog.result.Result.prototype.getState = function() {}; |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* @return {*} The value of this Result. Will return undefined if the Result is |
| 79 |
* pending or was an error. |
| 80 |
*/ |
| 81 |
goog.result.Result.prototype.getValue = function() {}; |
| 82 |
|
| 83 |
|
| 84 |
/** |
| 85 |
* @return {*} The error slug for this Result. Will return undefined if the |
| 86 |
* Result was a success, the error slug was not set, or if the Result is |
| 87 |
* pending. |
| 88 |
*/ |
| 89 |
goog.result.Result.prototype.getError = function() {}; |
| 90 |
|
| 91 |
|
| 92 |
/** |
| 93 |
* Cancels the current Result, invoking the canceler function, if set. |
| 94 |
* |
| 95 |
* @return {boolean} Whether the Result was canceled. |
| 96 |
*/ |
| 97 |
goog.result.Result.prototype.cancel = function() {}; |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* @return {boolean} Whether this Result was canceled. |
| 102 |
*/ |
| 103 |
goog.result.Result.prototype.isCanceled = function() {}; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* The value to be passed to the error handlers invoked upon cancellation. |
| 109 |
* @constructor |
| 110 |
* @extends {Error} |
| 111 |
* @final |
| 112 |
* @deprecated Use {@link goog.Promise} instead - http://go/promisemigration |
| 113 |
*/ |
| 114 |
goog.result.Result.CancelError = function() { |
| 115 |
// Note that this does not derive from goog.debug.Error in order to prevent |
| 116 |
// stack trace capture and reduce the amount of garbage generated during a |
| 117 |
// cancel() operation. |
| 118 |
}; |
| 119 |
goog.inherits(goog.result.Result.CancelError, Error); |