index.js 557 B

123456789101112131415161718192021222324252627
  1. 'use strict';
  2. class PLazy extends Promise {
  3. constructor(executor) {
  4. super(resolve => {
  5. resolve();
  6. });
  7. this._executor = executor;
  8. this._promise = null;
  9. }
  10. static from(fn) {
  11. return new PLazy(resolve => {
  12. resolve(fn());
  13. });
  14. }
  15. then(onFulfilled, onRejected) {
  16. this._promise = this._promise || new Promise(this._executor);
  17. return this._promise.then(onFulfilled, onRejected);
  18. }
  19. catch(onRejected) {
  20. this._promise = this._promise || new Promise(this._executor);
  21. return this._promise.catch(onRejected);
  22. }
  23. }
  24. module.exports = PLazy;