| 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 An adaptor from a Result to a Deferred. |
| 17 |
* |
| 18 |
* TODO (vbhasin): cancel() support. |
| 19 |
* TODO (vbhasin): See if we can make this a static. |
| 20 |
* TODO (gboyer, vbhasin): Rename to "Adapter" once this graduates; this is the |
| 21 |
* proper programmer spelling. |
| 22 |
*/ |
| 23 |
|
| 24 |
|
| 25 |
goog.provide('goog.result.DeferredAdaptor'); |
| 26 |
|
| 27 |
goog.require('goog.async.Deferred'); |
| 28 |
goog.require('goog.result'); |
| 29 |
goog.require('goog.result.Result'); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* An adaptor from Result to a Deferred, for use with existing Deferred chains. |
| 35 |
* |
| 36 |
* @param {!goog.result.Result} result A result. |
| 37 |
* @constructor |
| 38 |
* @extends {goog.async.Deferred} |
| 39 |
* @final |
| 40 |
* @deprecated Use {@link goog.Promise} instead - http://go/promisemigration |
| 41 |
*/ |
| 42 |
goog.result.DeferredAdaptor = function(result) { |
| 43 |
goog.result.DeferredAdaptor.base(this, 'constructor'); |
| 44 |
goog.result.wait(result, function(result) { |
| 45 |
if (this.hasFired()) { |
| 46 |
return; |
| 47 |
} |
| 48 |
if (result.getState() == goog.result.Result.State.SUCCESS) { |
| 49 |
this.callback(result.getValue()); |
| 50 |
} else if (result.getState() == goog.result.Result.State.ERROR) { |
| 51 |
if (result.getError() instanceof goog.result.Result.CancelError) { |
| 52 |
this.cancel(); |
| 53 |
} else { |
| 54 |
this.errback(result.getError()); |
| 55 |
} |
| 56 |
} |
| 57 |
}, this); |
| 58 |
}; |
| 59 |
goog.inherits(goog.result.DeferredAdaptor, goog.async.Deferred); |