UNPKG

lazy-promise

Version:

A bare-bones lazily-evaluated Promises/A+ implementation.

68 lines (57 loc) 2.74 kB
"use strict"; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } Object.defineProperty(exports, "__esModule", { value: true }); var asap = require('asap'); var LazyPromise = function () { function LazyPromise(executor) { _classCallCheck(this, LazyPromise); this._executor = executor; } /** * Attaches callbacks for the resolution and/or rejection of the Promise. * @param onfulfilled The callback to execute when the Promise is resolved. * @param onrejected The callback to execute when the Promise is rejected. * @returns A Promise for the completion of which ever callback is executed. */ _createClass(LazyPromise, [{ key: "then", value: function then(onfulfilled, onrejected) { var _this = this; if (!this._result) { this._result = new Promise(function (resolve, reject) { asap(function () { try { _this._executor(resolve, reject); } catch (ex) { reject(ex); } }); }); } return this._result.then(onfulfilled, onrejected); } /** * Attaches a callback for only the rejection of the Promise. * @param onrejected The callback to execute when the Promise is rejected. * @returns A Promise for the completion of the callback. */ }, { key: "catch", value: function _catch(onrejected) { return this.then(undefined, onrejected); } }]); return LazyPromise; }(); exports.default = LazyPromise; if (Object.setPrototypeOf) { Object.setPrototypeOf(LazyPromise.prototype, Promise.prototype); Object.setPrototypeOf(LazyPromise, Promise); } else { LazyPromise.prototype.__proto__ = Promise.prototype; LazyPromise.__proto__ = Promise; } module.exports = LazyPromise; module.exports.default = LazyPromise; //# sourceMappingURL=index.js.map