diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-05-03 15:35:00 +0200 |
commit | de98e0b232509d5f40c135d540a70e415272ff85 (patch) | |
tree | a79222a5b58484ab3b80d18efcaaa7ccc4769b33 /node_modules/fbjs/lib/Deferred.js | |
parent | e0c9d480a73fa629c1e4a47d3e721f1d2d345406 (diff) |
node_modules
Diffstat (limited to 'node_modules/fbjs/lib/Deferred.js')
-rw-r--r-- | node_modules/fbjs/lib/Deferred.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/node_modules/fbjs/lib/Deferred.js b/node_modules/fbjs/lib/Deferred.js new file mode 100644 index 000000000..bfc358580 --- /dev/null +++ b/node_modules/fbjs/lib/Deferred.js @@ -0,0 +1,81 @@ +"use strict"; + +var Promise = require("./Promise"); + +function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } + +/** + * Copyright (c) 2013-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + * + * @typechecks + * + */ + +/** + * Deferred provides a Promise-like API that exposes methods to resolve and + * reject the Promise. It is most useful when converting non-Promise code to use + * Promises. + * + * If you want to export the Promise without exposing access to the resolve and + * reject methods, you should export `getPromise` which returns a Promise with + * the same semantics excluding those methods. + */ +var Deferred = function () { + function Deferred() { + var _this = this; + + _classCallCheck(this, Deferred); + + this._settled = false; + this._promise = new Promise(function (resolve, reject) { + _this._resolve = resolve; + _this._reject = reject; + }); + } + + Deferred.prototype.getPromise = function getPromise() { + return this._promise; + }; + + Deferred.prototype.resolve = function resolve(value) { + this._settled = true; + this._resolve(value); + }; + + Deferred.prototype.reject = function reject(reason) { + this._settled = true; + this._reject(reason); + }; + + Deferred.prototype["catch"] = function _catch() { + return Promise.prototype["catch"].apply(this._promise, arguments); + }; + + Deferred.prototype.then = function then() { + return Promise.prototype.then.apply(this._promise, arguments); + }; + + Deferred.prototype.done = function done() { + // Embed the polyfill for the non-standard Promise.prototype.done so that + // users of the open source fbjs don't need a custom lib for Promise + var promise = arguments.length ? this._promise.then.apply(this._promise, arguments) : this._promise; + promise.then(undefined, function (err) { + setTimeout(function () { + throw err; + }, 0); + }); + }; + + Deferred.prototype.isSettled = function isSettled() { + return this._settled; + }; + + return Deferred; +}(); + +module.exports = Deferred;
\ No newline at end of file |