Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. declare class CancelErrorClass extends Error {
  2. readonly name: 'CancelError';
  3. readonly isCanceled: true;
  4. constructor(reason?: string);
  5. }
  6. declare namespace PCancelable {
  7. /**
  8. Accepts a function that is called when the promise is canceled.
  9. You're not required to call this function. You can call this function multiple times to add multiple cancel handlers.
  10. */
  11. interface OnCancelFunction {
  12. (cancelHandler: () => void): void;
  13. shouldReject: boolean;
  14. }
  15. type CancelError = CancelErrorClass;
  16. }
  17. declare class PCancelable<ValueType> extends Promise<ValueType> {
  18. /**
  19. Convenience method to make your promise-returning or async function cancelable.
  20. @param fn - A promise-returning function. The function you specify will have `onCancel` appended to its parameters.
  21. @example
  22. ```
  23. import PCancelable = require('p-cancelable');
  24. const fn = PCancelable.fn((input, onCancel) => {
  25. const job = new Job();
  26. onCancel(() => {
  27. job.cleanup();
  28. });
  29. return job.start(); //=> Promise
  30. });
  31. const cancelablePromise = fn('input'); //=> PCancelable
  32. // …
  33. cancelablePromise.cancel();
  34. ```
  35. */
  36. static fn<ReturnType>(
  37. userFn: (onCancel: PCancelable.OnCancelFunction) => PromiseLike<ReturnType>
  38. ): () => PCancelable<ReturnType>;
  39. static fn<Agument1Type, ReturnType>(
  40. userFn: (
  41. argument1: Agument1Type,
  42. onCancel: PCancelable.OnCancelFunction
  43. ) => PromiseLike<ReturnType>
  44. ): (argument1: Agument1Type) => PCancelable<ReturnType>;
  45. static fn<Agument1Type, Agument2Type, ReturnType>(
  46. userFn: (
  47. argument1: Agument1Type,
  48. argument2: Agument2Type,
  49. onCancel: PCancelable.OnCancelFunction
  50. ) => PromiseLike<ReturnType>
  51. ): (
  52. argument1: Agument1Type,
  53. argument2: Agument2Type
  54. ) => PCancelable<ReturnType>;
  55. static fn<Agument1Type, Agument2Type, Agument3Type, ReturnType>(
  56. userFn: (
  57. argument1: Agument1Type,
  58. argument2: Agument2Type,
  59. argument3: Agument3Type,
  60. onCancel: PCancelable.OnCancelFunction
  61. ) => PromiseLike<ReturnType>
  62. ): (
  63. argument1: Agument1Type,
  64. argument2: Agument2Type,
  65. argument3: Agument3Type
  66. ) => PCancelable<ReturnType>;
  67. static fn<Agument1Type, Agument2Type, Agument3Type, Agument4Type, ReturnType>(
  68. userFn: (
  69. argument1: Agument1Type,
  70. argument2: Agument2Type,
  71. argument3: Agument3Type,
  72. argument4: Agument4Type,
  73. onCancel: PCancelable.OnCancelFunction
  74. ) => PromiseLike<ReturnType>
  75. ): (
  76. argument1: Agument1Type,
  77. argument2: Agument2Type,
  78. argument3: Agument3Type,
  79. argument4: Agument4Type
  80. ) => PCancelable<ReturnType>;
  81. static fn<
  82. Agument1Type,
  83. Agument2Type,
  84. Agument3Type,
  85. Agument4Type,
  86. Agument5Type,
  87. ReturnType
  88. >(
  89. userFn: (
  90. argument1: Agument1Type,
  91. argument2: Agument2Type,
  92. argument3: Agument3Type,
  93. argument4: Agument4Type,
  94. argument5: Agument5Type,
  95. onCancel: PCancelable.OnCancelFunction
  96. ) => PromiseLike<ReturnType>
  97. ): (
  98. argument1: Agument1Type,
  99. argument2: Agument2Type,
  100. argument3: Agument3Type,
  101. argument4: Agument4Type,
  102. argument5: Agument5Type
  103. ) => PCancelable<ReturnType>;
  104. static fn<ReturnType>(
  105. userFn: (...arguments: unknown[]) => PromiseLike<ReturnType>
  106. ): (...arguments: unknown[]) => PCancelable<ReturnType>;
  107. /**
  108. Create a promise that can be canceled.
  109. Can be constructed in the same was as a [`Promise` constructor](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise), but with an appended `onCancel` parameter in `executor`. `PCancelable` is a subclass of `Promise`.
  110. Cancelling will reject the promise with `CancelError`. To avoid that, set `onCancel.shouldReject` to `false`.
  111. @example
  112. ```
  113. import PCancelable = require('p-cancelable');
  114. const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
  115. const job = new Job();
  116. onCancel.shouldReject = false;
  117. onCancel(() => {
  118. job.stop();
  119. });
  120. job.on('finish', resolve);
  121. });
  122. cancelablePromise.cancel(); // Doesn't throw an error
  123. ```
  124. */
  125. constructor(
  126. executor: (
  127. resolve: (value?: ValueType | PromiseLike<ValueType>) => void,
  128. reject: (reason?: unknown) => void,
  129. onCancel: PCancelable.OnCancelFunction
  130. ) => void
  131. );
  132. /**
  133. Whether the promise is canceled.
  134. */
  135. readonly isCanceled: boolean;
  136. /**
  137. Cancel the promise and optionally provide a reason.
  138. The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.
  139. @param reason - The cancellation reason to reject the promise with.
  140. */
  141. cancel: (reason?: string) => void;
  142. /**
  143. Rejection reason when `.cancel()` is called.
  144. It includes a `.isCanceled` property for convenience.
  145. */
  146. static CancelError: typeof CancelErrorClass;
  147. }
  148. export = PCancelable;