deferredadaptor.js 1.9 KB

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