index.d.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. export interface SynchronousPromise<T> extends Promise<T> {
  2. pause: () => SynchronousPromise<T>
  3. resume: () => SynchronousPromise<T>
  4. }
  5. export type ValueOrPromiseOfValue<T> = T | PromiseLike<T>
  6. export type RejectedOutcome = {
  7. status: "rejected",
  8. reason: any
  9. }
  10. export type FulfilledOutcome<T> = {
  11. status: "fulfilled",
  12. value: T
  13. }
  14. export type SettledOutcome<T> = FulfilledOutcome<T> | RejectedOutcome
  15. export interface SynchronousPromiseConstructor {
  16. /**
  17. * A reference to the prototype.
  18. */
  19. prototype: SynchronousPromise<any>;
  20. /**
  21. * Creates a new Promise.
  22. * @param executor A callback used to initialize the promise. This callback is passed two arguments:
  23. * a resolve callback used resolve the promise with a value or the result of another promise,
  24. * and a reject callback used to reject the promise with a provided reason or error.
  25. */
  26. new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): SynchronousPromise<T>;
  27. /**
  28. * Creates a Promise that is resolved with an array of results when all of the provided Promises
  29. * resolve, or rejected when any Promise is rejected.
  30. * @param v1 An array of Promises
  31. * @returns A new Promise.
  32. */
  33. all<T>(v1: ValueOrPromiseOfValue<T>[]): SynchronousPromise<T[]>;
  34. /**
  35. * Creates a Promise that is resolved with an array of results when all of the provided Promises
  36. * resolve, or rejected when any Promise is rejected.
  37. * @param values Any number of Promises.
  38. * @returns A new Promise.
  39. */
  40. all<T>(...values: ValueOrPromiseOfValue<T>[]): SynchronousPromise<T[]>;
  41. /**
  42. * Creates a Promise that is resolved with an array of outcome objects after all of the provided Promises
  43. * have settled. Each outcome object has a .status of either "fulfilled" or "rejected" and corresponding
  44. * "value" or "reason" properties.
  45. * @param v1 An array of Promises.
  46. * @returns A new Promise.
  47. */
  48. allSettled<T>(v1: ValueOrPromiseOfValue<T>[]): SynchronousPromise<SettledOutcome<T>[]>;
  49. /**
  50. * Creates a Promise that is resolved with an array of outcome objects after all of the provided Promises
  51. * have settled. Each outcome object has a .status of either "fulfilled" or "rejected" and corresponding
  52. * "value" or "reason" properties.
  53. * @param values Any number of promises
  54. * @returns A new Promise.
  55. */
  56. allSettled<TAllSettled>(...values: ValueOrPromiseOfValue<TAllSettled>[]): SynchronousPromise<SettledOutcome<TAllSettled>[]>;
  57. /**
  58. * Creates a Promise that is resolved or rejected when any of the provided Promises are resolved
  59. * or rejected.
  60. * @param values An array of Promises.
  61. * @returns A new Promise.
  62. */
  63. // race<T>(values: IterableShim<T | PromiseLike<T>>): Promise<T>;
  64. /**
  65. * Creates a Promise that is resolved with the first value from the provided
  66. * Promises, or rejected when all provided Promises reject
  67. * @param v1 An array of Promises
  68. */
  69. any<T>(v1: ValueOrPromiseOfValue<T>[]): SynchronousPromise<T>;
  70. /**
  71. * Creates a Promise that is resolved with the first value from the provided
  72. * Promises, or rejected when all provided Promises reject
  73. * @param values Any number of Promises
  74. */
  75. any<T>(...values: ValueOrPromiseOfValue<T>[]): SynchronousPromise<T>;
  76. /**
  77. * Creates a new rejected promise for the provided reason.
  78. * @param reason The reason the promise was rejected.
  79. * @returns A new rejected Promise.
  80. */
  81. reject(reason: any): SynchronousPromise<void>;
  82. /**
  83. * Creates a new rejected promise for the provided reason.
  84. * @param reason The reason the promise was rejected.
  85. * @returns A new rejected Promise.
  86. */
  87. reject<T>(reason: any): SynchronousPromise<T>;
  88. /**
  89. * Creates a new resolved promise for the provided value.
  90. * @param value A promise.
  91. * @returns A promise whose internal state matches the provided promise.
  92. */
  93. resolve<T>(value: T | PromiseLike<T>): SynchronousPromise<T>;
  94. /**
  95. * Creates a new resolved promise .
  96. * @returns A resolved promise.
  97. */
  98. resolve(): SynchronousPromise<void>;
  99. /**
  100. * Creates a new unresolved promise with the `resolve` and `reject` methods exposed
  101. * @returns An unresolved promise with the `resolve` and `reject` methods exposed
  102. */
  103. unresolved<T>(): UnresolvedSynchronousPromise<T>;
  104. /**
  105. * Installs SynchronousPromise as the global Promise implementation.
  106. * When running from within typescript, you will need to use this to
  107. * patch the generated __awaiter to ensure it gets a _real_ Promise implementation
  108. * (see https://github.com/Microsoft/TypeScript/issues/19909).
  109. *
  110. * Use the following code:
  111. * declare var __awaiter: Function;
  112. * __awaiter = SynchronousPromise.installGlobally();
  113. *
  114. * This is non-destructive to the __awaiter: it simply wraps it in a closure
  115. * where the real implementation of Promise has already been captured.
  116. */
  117. installGlobally(__awaiter: Function): Function;
  118. /*
  119. * Uninstalls SynchronousPromise as the global Promise implementation,
  120. * if it is already installed.
  121. */
  122. uninstallGlobally(): void;
  123. }
  124. /**
  125. * Interface type only exposed when using the static unresolved() convenience method
  126. */
  127. interface UnresolvedSynchronousPromise<T> extends SynchronousPromise<T> {
  128. resolve<T>(data: T): void;
  129. resolve(): void;
  130. reject<T>(data: T): void;
  131. }
  132. export var SynchronousPromise: SynchronousPromiseConstructor;