Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.d.ts 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Accepts a function that is called when the promise is canceled.
  3. *
  4. * You're not required to call this function. You can call this function multiple times to add multiple cancel handlers.
  5. */
  6. export interface OnCancelFunction {
  7. (cancelHandler: () => void): void;
  8. shouldReject: boolean;
  9. }
  10. declare class PCancelable<ValueType> extends Promise<ValueType> {
  11. /**
  12. * Convenience method to make your promise-returning or async function cancelable.
  13. *
  14. * @param fn - A promise-returning function. The function you specify will have `onCancel` appended to its parameters.
  15. *
  16. * @example
  17. *
  18. * import PCancelable from 'p-cancelable';
  19. *
  20. * const fn = PCancelable.fn((input, onCancel) => {
  21. * const job = new Job();
  22. *
  23. * onCancel(() => {
  24. * job.cleanup();
  25. * });
  26. *
  27. * return job.start(); //=> Promise
  28. * });
  29. *
  30. * const cancelablePromise = fn('input'); //=> PCancelable
  31. *
  32. * // …
  33. *
  34. * cancelablePromise.cancel();
  35. */
  36. static fn<ReturnType>(
  37. userFn: (onCancel: OnCancelFunction) => PromiseLike<ReturnType>
  38. ): () => PCancelable<ReturnType>;
  39. static fn<Agument1Type, ReturnType>(
  40. userFn: (
  41. argument1: Agument1Type,
  42. onCancel: 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: 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: 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: 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: 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. *
  110. * 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`.
  111. *
  112. * Cancelling will reject the promise with `CancelError`. To avoid that, set `onCancel.shouldReject` to `false`.
  113. *
  114. * @example
  115. *
  116. * import PCancelable from 'p-cancelable';
  117. *
  118. * const cancelablePromise = new PCancelable((resolve, reject, onCancel) => {
  119. * const job = new Job();
  120. *
  121. * onCancel.shouldReject = false;
  122. * onCancel(() => {
  123. * job.stop();
  124. * });
  125. *
  126. * job.on('finish', resolve);
  127. * });
  128. *
  129. * cancelablePromise.cancel(); // Doesn't throw an error
  130. */
  131. constructor(
  132. executor: (
  133. resolve: (value?: ValueType | PromiseLike<ValueType>) => void,
  134. reject: (reason?: unknown) => void,
  135. onCancel: OnCancelFunction
  136. ) => void
  137. );
  138. /**
  139. * Whether the promise is canceled.
  140. */
  141. readonly isCanceled: boolean;
  142. /**
  143. * Cancel the promise and optionally provide a reason.
  144. *
  145. * The cancellation is synchronous. Calling it after the promise has settled or multiple times does nothing.
  146. *
  147. * @param reason - The cancellation reason to reject the promise with.
  148. */
  149. cancel(reason?: string): void;
  150. }
  151. export default PCancelable;
  152. /**
  153. * Rejection reason when `.cancel()` is called.
  154. *
  155. * It includes a `.isCanceled` property for convenience.
  156. */
  157. export class CancelError extends Error {
  158. readonly name: 'CancelError';
  159. readonly isCanceled: true;
  160. constructor(reason?: string);
  161. }