index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. 'use strict';
  2. class CancelError extends Error {
  3. constructor() {
  4. super('Promise was canceled');
  5. this.name = 'CancelError';
  6. }
  7. get isCanceled() {
  8. return true;
  9. }
  10. }
  11. class PCancelable {
  12. static fn(userFn) {
  13. return function () {
  14. const args = [].slice.apply(arguments);
  15. return new PCancelable((resolve, reject, onCancel) => {
  16. args.push(onCancel);
  17. userFn.apply(null, args).then(resolve, reject);
  18. });
  19. };
  20. }
  21. constructor(executor) {
  22. this._cancelHandlers = [];
  23. this._isPending = true;
  24. this._isCanceled = false;
  25. this._promise = new Promise((resolve, reject) => {
  26. this._reject = reject;
  27. return executor(
  28. value => {
  29. this._isPending = false;
  30. resolve(value);
  31. },
  32. error => {
  33. this._isPending = false;
  34. reject(error);
  35. },
  36. handler => {
  37. this._cancelHandlers.push(handler);
  38. }
  39. );
  40. });
  41. }
  42. then(onFulfilled, onRejected) {
  43. return this._promise.then(onFulfilled, onRejected);
  44. }
  45. catch(onRejected) {
  46. return this._promise.catch(onRejected);
  47. }
  48. finally(onFinally) {
  49. return this._promise.finally(onFinally);
  50. }
  51. cancel() {
  52. if (!this._isPending || this._isCanceled) {
  53. return;
  54. }
  55. if (this._cancelHandlers.length > 0) {
  56. try {
  57. for (const handler of this._cancelHandlers) {
  58. handler();
  59. }
  60. } catch (err) {
  61. this._reject(err);
  62. }
  63. }
  64. this._isCanceled = true;
  65. this._reject(new CancelError());
  66. }
  67. get isCanceled() {
  68. return this._isCanceled;
  69. }
  70. }
  71. Object.setPrototypeOf(PCancelable.prototype, Promise.prototype);
  72. module.exports = PCancelable;
  73. module.exports.CancelError = CancelError;