deb83f91143e0484c1c3fd3a75512a08caf1c98bc406d41337578f47daee07234fbbd87b2ad8a893d9527771250759be3640df033e21b216d188cb259df84e 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. export = Q;
  2. export as namespace Q;
  3. /**
  4. * If value is a Q promise, returns the promise.
  5. * If value is a promise from another library it is coerced into a Q promise (where possible).
  6. * If value is not a promise, returns a promise that is fulfilled with value.
  7. */
  8. declare function Q<T>(promise: PromiseLike<T> | T): Q.Promise<T>;
  9. /**
  10. * Calling with nothing at all creates a void promise
  11. */
  12. declare function Q(): Q.Promise<void>;
  13. declare namespace Q {
  14. export type IWhenable<T> = PromiseLike<T> | T;
  15. export type IPromise<T> = PromiseLike<T>;
  16. export interface Deferred<T> {
  17. promise: Promise<T>;
  18. /**
  19. * Calling resolve with a pending promise causes promise to wait on the passed promise, becoming fulfilled with its
  20. * fulfillment value or rejected with its rejection reason (or staying pending forever, if the passed promise does).
  21. * Calling resolve with a rejected promise causes promise to be rejected with the passed promise's rejection reason.
  22. * Calling resolve with a fulfilled promise causes promise to be fulfilled with the passed promise's fulfillment value.
  23. * Calling resolve with a non-promise value causes promise to be fulfilled with that value.
  24. */
  25. resolve(value?: IWhenable<T>): void;
  26. /**
  27. * Calling reject with a reason causes promise to be rejected with that reason.
  28. */
  29. reject(reason?: any): void;
  30. /**
  31. * Calling notify with a value causes promise to be notified of progress with that value. That is, any onProgress
  32. * handlers registered with promise or promises derived from promise will be called with the progress value.
  33. */
  34. notify(value: any): void;
  35. /**
  36. * Returns a function suitable for passing to a Node.js API. That is, it has a signature (err, result) and will
  37. * reject deferred.promise with err if err is given, or fulfill it with result if that is given.
  38. */
  39. makeNodeResolver(): (reason: any, value: T) => void;
  40. }
  41. export interface Promise<T> {
  42. /**
  43. * The then method from the Promises/A+ specification, with an additional progress handler.
  44. */
  45. then<U>(
  46. onFulfill?: ((value: T) => IWhenable<U>) | null,
  47. onReject?: ((error: any) => IWhenable<U>) | null,
  48. onProgress?: ((progress: any) => any) | null,
  49. ): Promise<U>;
  50. then<U = T, V = never>(
  51. onFulfill?: ((value: T) => IWhenable<U>) | null,
  52. onReject?: ((error: any) => IWhenable<V>) | null,
  53. onProgress?: ((progress: any) => any) | null,
  54. ): Promise<U | V>;
  55. /**
  56. * Like a finally clause, allows you to observe either the fulfillment or rejection of a promise, but to do so
  57. * without modifying the final value. This is useful for collecting resources regardless of whether a job succeeded,
  58. * like closing a database connection, shutting a server down, or deleting an unneeded key from an object.
  59. * finally returns a promise, which will become resolved with the same fulfillment value or rejection reason
  60. * as promise. However, if callback returns a promise, the resolution of the returned promise will be delayed
  61. * until the promise returned from callback is finished. Furthermore, if the returned promise rejects, that
  62. * rejection will be passed down the chain instead of the previous result.
  63. */
  64. finally(finallyCallback: () => any): Promise<T>;
  65. /**
  66. * Alias for finally() (for non-ES5 browsers)
  67. */
  68. fin(finallyCallback: () => any): Promise<T>;
  69. /**
  70. * Like then, but "spreads" the array into a variadic fulfillment handler. If any of the promises in the array are
  71. * rejected, instead calls onRejected with the first rejected promise's rejection reason.
  72. * This is especially useful in conjunction with all
  73. */
  74. spread<U>(onFulfill: (...args: any[]) => IWhenable<U>, onReject?: (reason: any) => IWhenable<U>): Promise<U>;
  75. /**
  76. * A sugar method, equivalent to promise.then(undefined, onRejected).
  77. */
  78. catch<U>(onRejected: (reason: any) => IWhenable<U>): Promise<U>;
  79. /**
  80. * Alias for catch() (for non-ES5 browsers)
  81. */
  82. fail<U>(onRejected: (reason: any) => IWhenable<U>): Promise<U>;
  83. /**
  84. * A sugar method, equivalent to promise.then(undefined, undefined, onProgress).
  85. */
  86. progress(onProgress: (progress: any) => any): Promise<T>;
  87. /**
  88. * Much like then, but with different behavior around unhandled rejection. If there is an unhandled rejection,
  89. * either because promise is rejected and no onRejected callback was provided, or because onFulfilled or onRejected
  90. * threw an error or returned a rejected promise, the resulting rejection reason is thrown as an exception in a
  91. * future turn of the event loop.
  92. * This method should be used to terminate chains of promises that will not be passed elsewhere. Since exceptions
  93. * thrown in then callbacks are consumed and transformed into rejections, exceptions at the end of the chain are
  94. * easy to accidentally, silently ignore. By arranging for the exception to be thrown in a future turn of the
  95. * event loop, so that it won't be caught, it causes an onerror event on the browser window, or an uncaughtException
  96. * event on Node.js's process object.
  97. * Exceptions thrown by done will have long stack traces, if Q.longStackSupport is set to true. If Q.onerror is set,
  98. * exceptions will be delivered there instead of thrown in a future turn.
  99. * The Golden Rule of done vs. then usage is: either return your promise to someone else, or if the chain ends
  100. * with you, call done to terminate it. Terminating with catch is not sufficient because the catch handler may
  101. * itself throw an error.
  102. */
  103. done(
  104. onFulfilled?: ((value: T) => any) | null,
  105. onRejected?: ((reason: any) => any) | null,
  106. onProgress?: ((progress: any) => any) | null,
  107. ): void;
  108. /**
  109. * If callback is a function, assumes it's a Node.js-style callback, and calls it as either callback(rejectionReason)
  110. * when/if promise becomes rejected, or as callback(null, fulfillmentValue) when/if promise becomes fulfilled.
  111. * If callback is not a function, simply returns promise.
  112. */
  113. nodeify(callback: (reason: any, value: any) => void): Promise<T>;
  114. /**
  115. * Returns a promise to get the named property of an object. Essentially equivalent to
  116. *
  117. * @example
  118. * promise.then(function (o) { return o[propertyName]; });
  119. */
  120. get<U>(propertyName: string): Promise<U>;
  121. set<U>(propertyName: string, value: any): Promise<U>;
  122. delete<U>(propertyName: string): Promise<U>;
  123. /**
  124. * Returns a promise for the result of calling the named method of an object with the given array of arguments.
  125. * The object itself is this in the function, just like a synchronous method call. Essentially equivalent to
  126. *
  127. * @example
  128. * promise.then(function (o) { return o[methodName].apply(o, args); });
  129. */
  130. post<U>(methodName: string, args: any[]): Promise<U>;
  131. /**
  132. * Returns a promise for the result of calling the named method of an object with the given variadic arguments.
  133. * The object itself is this in the function, just like a synchronous method call.
  134. */
  135. invoke<U>(methodName: string, ...args: any[]): Promise<U>;
  136. /**
  137. * Returns a promise for an array of the property names of an object. Essentially equivalent to
  138. *
  139. * @example
  140. * promise.then(function (o) { return Object.keys(o); });
  141. */
  142. keys(): Promise<string[]>;
  143. /**
  144. * Returns a promise for the result of calling a function, with the given array of arguments. Essentially equivalent to
  145. *
  146. * @example
  147. * promise.then(function (f) {
  148. * return f.apply(undefined, args);
  149. * });
  150. */
  151. fapply<U>(args: any[]): Promise<U>;
  152. /**
  153. * Returns a promise for the result of calling a function, with the given variadic arguments. Has the same return
  154. * value/thrown exception translation as explained above for fbind.
  155. * In its static form, it is aliased as Q.try, since it has semantics similar to a try block (but handling both
  156. * synchronous exceptions and asynchronous rejections). This allows code like
  157. *
  158. * @example
  159. * Q.try(function () {
  160. * if (!isConnectedToCloud()) {
  161. * throw new Error("The cloud is down!");
  162. * }
  163. * return syncToCloud();
  164. * })
  165. * .catch(function (error) {
  166. * console.error("Couldn't sync to the cloud", error);
  167. * });
  168. */
  169. fcall<U>(...args: any[]): Promise<U>;
  170. /**
  171. * A sugar method, equivalent to promise.then(function () { return value; }).
  172. */
  173. thenResolve<U>(value: U): Promise<U>;
  174. /**
  175. * A sugar method, equivalent to promise.then(function () { throw reason; }).
  176. */
  177. thenReject<U = T>(reason?: any): Promise<U>;
  178. /**
  179. * Attaches a handler that will observe the value of the promise when it becomes fulfilled, returning a promise for
  180. * that same value, perhaps deferred but not replaced by the promise returned by the onFulfilled handler.
  181. */
  182. tap(onFulfilled: (value: T) => any): Promise<T>;
  183. /**
  184. * Returns a promise that will have the same result as promise, except that if promise is not fulfilled or rejected
  185. * before ms milliseconds, the returned promise will be rejected with an Error with the given message. If message
  186. * is not supplied, the message will be "Timed out after " + ms + " ms".
  187. */
  188. timeout(ms: number, message?: string): Promise<T>;
  189. /**
  190. * Returns a promise that will have the same result as promise, but will only be fulfilled or rejected after at least
  191. * ms milliseconds have passed.
  192. */
  193. delay(ms: number): Promise<T>;
  194. /**
  195. * Returns whether a given promise is in the fulfilled state. When the static version is used on non-promises, the
  196. * result is always true.
  197. */
  198. isFulfilled(): boolean;
  199. /**
  200. * Returns whether a given promise is in the rejected state. When the static version is used on non-promises, the
  201. * result is always false.
  202. */
  203. isRejected(): boolean;
  204. /**
  205. * Returns whether a given promise is in the pending state. When the static version is used on non-promises, the
  206. * result is always false.
  207. */
  208. isPending(): boolean;
  209. valueOf(): any;
  210. /**
  211. * Returns a "state snapshot" object, which will be in one of three forms:
  212. *
  213. * - { state: "pending" }
  214. * - { state: "fulfilled", value: <fulfllment value> }
  215. * - { state: "rejected", reason: <rejection reason> }
  216. */
  217. inspect(): PromiseState<T>;
  218. }
  219. export interface PromiseState<T> {
  220. state: "fulfilled" | "rejected" | "pending";
  221. value?: T | undefined;
  222. reason?: any;
  223. }
  224. /**
  225. * Returns a "deferred" object with a:
  226. * promise property
  227. * resolve(value) method
  228. * reject(reason) method
  229. * notify(value) method
  230. * makeNodeResolver() method
  231. */
  232. export function defer<T>(): Deferred<T>;
  233. /**
  234. * Calling resolve with a pending promise causes promise to wait on the passed promise, becoming fulfilled with its
  235. * fulfillment value or rejected with its rejection reason (or staying pending forever, if the passed promise does).
  236. * Calling resolve with a rejected promise causes promise to be rejected with the passed promise's rejection reason.
  237. * Calling resolve with a fulfilled promise causes promise to be fulfilled with the passed promise's fulfillment value.
  238. * Calling resolve with a non-promise value causes promise to be fulfilled with that value.
  239. */
  240. export function resolve<T>(object?: IWhenable<T>): Promise<T>;
  241. /**
  242. * Returns a promise that is rejected with reason.
  243. */
  244. export function reject<T>(reason?: any): Promise<T>;
  245. // If no value provided, returned promise will be of void type
  246. export function when(): Promise<void>;
  247. // if no fulfill, reject, or progress provided, returned promise will be of same type
  248. export function when<T>(value: IWhenable<T>): Promise<T>;
  249. // If a non-promise value is provided, it will not reject or progress
  250. export function when<T, U>(
  251. value: IWhenable<T>,
  252. onFulfilled: (val: T) => IWhenable<U>,
  253. onRejected?: ((reason: any) => IWhenable<U>) | null,
  254. onProgress?: ((progress: any) => any) | null,
  255. ): Promise<U>;
  256. /**
  257. * (Deprecated) Returns a new function that calls a function asynchronously with the given variadic arguments, and returns a promise.
  258. * Notably, any synchronous return values or thrown exceptions are transformed, respectively, into fulfillment values
  259. * or rejection reasons for the promise returned by this new function.
  260. * This method is especially useful in its static form for wrapping functions to ensure that they are always
  261. * asynchronous, and that any thrown exceptions (intentional or accidental) are appropriately transformed into a
  262. * returned rejected promise. For example:
  263. *
  264. * @example
  265. * var getUserData = Q.fbind(function (userName) {
  266. * if (!userName) {
  267. * throw new Error("userName must be truthy!");
  268. * }
  269. * if (localCache.has(userName)) {
  270. * return localCache.get(userName);
  271. * }
  272. * return getUserFromCloud(userName);
  273. * });
  274. */
  275. export function fbind<T>(method: (...args: any[]) => IWhenable<T>, ...args: any[]): (...args: any[]) => Promise<T>;
  276. /**
  277. * Returns a promise for the result of calling a function, with the given variadic arguments. Has the same return
  278. * value/thrown exception translation as explained above for fbind.
  279. * In its static form, it is aliased as Q.try, since it has semantics similar to a try block (but handling both synchronous
  280. * exceptions and asynchronous rejections). This allows code like
  281. *
  282. * @example
  283. * Q.try(function () {
  284. * if (!isConnectedToCloud()) {
  285. * throw new Error("The cloud is down!");
  286. * }
  287. * return syncToCloud();
  288. * })
  289. * .catch(function (error) {
  290. * console.error("Couldn't sync to the cloud", error);
  291. * });
  292. */
  293. export function fcall<T>(method: (...args: any[]) => T, ...args: any[]): Promise<T>;
  294. // but 'try' is a reserved word. This is the only way to get around this
  295. /**
  296. * Alias for fcall()
  297. */
  298. export { fcall as try };
  299. /**
  300. * Returns a promise for the result of calling the named method of an object with the given variadic arguments.
  301. * The object itself is this in the function, just like a synchronous method call.
  302. */
  303. export function invoke<T>(obj: any, functionName: string, ...args: any[]): Promise<T>;
  304. /**
  305. * Alias for invoke()
  306. */
  307. export function send<T>(obj: any, functionName: string, ...args: any[]): Promise<T>;
  308. /**
  309. * Alias for invoke()
  310. */
  311. export function mcall<T>(obj: any, functionName: string, ...args: any[]): Promise<T>;
  312. /**
  313. * Creates a promise-returning function from a Node.js-style function, optionally binding it with the given
  314. * variadic arguments. An example:
  315. *
  316. * @example
  317. * var readFile = Q.nfbind(FS.readFile);
  318. * readFile("foo.txt", "utf-8").done(function (text) {
  319. * //...
  320. * });
  321. *
  322. * Note that if you have a method that uses the Node.js callback pattern, as opposed to just a function, you will
  323. * need to bind its this value before passing it to nfbind, like so:
  324. *
  325. * @example
  326. * var Kitty = mongoose.model("Kitty");
  327. * var findKitties = Q.nfbind(Kitty.find.bind(Kitty));
  328. *
  329. * The better strategy for methods would be to use Q.nbind, as shown below.
  330. */
  331. export function nfbind<T>(nodeFunction: (...args: any[]) => any, ...args: any[]): (...args: any[]) => Promise<T>;
  332. /**
  333. * Alias for nfbind()
  334. */
  335. export function denodeify<T>(nodeFunction: (...args: any[]) => any, ...args: any[]): (...args: any[]) => Promise<T>;
  336. /**
  337. * Creates a promise-returning function from a Node.js-style method, optionally binding it with the given
  338. * variadic arguments. An example:
  339. *
  340. * @example
  341. * var Kitty = mongoose.model("Kitty");
  342. * var findKitties = Q.nbind(Kitty.find, Kitty);
  343. * findKitties({ cute: true }).done(function (theKitties) {
  344. * //...
  345. * });
  346. */
  347. export function nbind<T>(
  348. nodeFunction: (...args: any[]) => any,
  349. thisArg: any,
  350. ...args: any[]
  351. ): (...args: any[]) => Promise<T>;
  352. /**
  353. * Calls a Node.js-style function with the given array of arguments, returning a promise that is fulfilled if the
  354. * Node.js function calls back with a result, or rejected if it calls back with an error
  355. * (or throws one synchronously). An example:
  356. *
  357. * @example
  358. * Q.nfapply(FS.readFile, ["foo.txt", "utf-8"]).done(function (text) {
  359. * });
  360. *
  361. * Note that this example only works because FS.readFile is a function exported from a module, not a method on
  362. * an object. For methods, e.g. redisClient.get, you must bind the method to an instance before passing it to
  363. * Q.nfapply (or, generally, as an argument to any function call):
  364. *
  365. * @example
  366. * Q.nfapply(redisClient.get.bind(redisClient), ["user:1:id"]).done(function (user) {
  367. * });
  368. *
  369. * The better strategy for methods would be to use Q.npost, as shown below.
  370. */
  371. export function nfapply<T>(nodeFunction: (...args: any[]) => any, args: any[]): Promise<T>;
  372. /**
  373. * Calls a Node.js-style function with the given variadic arguments, returning a promise that is fulfilled if the
  374. * Node.js function calls back with a result, or rejected if it calls back with an error
  375. * (or throws one synchronously). An example:
  376. *
  377. * @example
  378. * Q.nfcall(FS.readFile, "foo.txt", "utf-8").done(function (text) {
  379. * });
  380. *
  381. * The same warning about functions vs. methods applies for nfcall as it does for nfapply. In this case, the better
  382. * strategy would be to use Q.ninvoke.
  383. */
  384. export function nfcall<T>(nodeFunction: (...args: any[]) => any, ...args: any[]): Promise<T>;
  385. /**
  386. * Calls a Node.js-style method with the given arguments array, returning a promise that is fulfilled if the method
  387. * calls back with a result, or rejected if it calls back with an error (or throws one synchronously). An example:
  388. *
  389. * @example
  390. * Q.npost(redisClient, "get", ["user:1:id"]).done(function (user) {
  391. * });
  392. */
  393. export function npost<T>(nodeModule: any, functionName: string, args: any[]): Promise<T>;
  394. /**
  395. * Calls a Node.js-style method with the given variadic arguments, returning a promise that is fulfilled if the
  396. * method calls back with a result, or rejected if it calls back with an error (or throws one synchronously). An example:
  397. *
  398. * @example
  399. * Q.ninvoke(redisClient, "get", "user:1:id").done(function (user) {
  400. * });
  401. */
  402. export function ninvoke<T>(nodeModule: any, functionName: string, ...args: any[]): Promise<T>;
  403. /**
  404. * Alias for ninvoke()
  405. */
  406. export function nsend<T>(nodeModule: any, functionName: string, ...args: any[]): Promise<T>;
  407. /**
  408. * Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected.
  409. */
  410. export function all<A, B, C, D, E, F>(
  411. promises: IWhenable<[IWhenable<A>, IWhenable<B>, IWhenable<C>, IWhenable<D>, IWhenable<E>, IWhenable<F>]>,
  412. ): Promise<[A, B, C, D, E, F]>;
  413. /**
  414. * Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected.
  415. */
  416. export function all<A, B, C, D, E>(
  417. promises: IWhenable<[IWhenable<A>, IWhenable<B>, IWhenable<C>, IWhenable<D>, IWhenable<E>]>,
  418. ): Promise<[A, B, C, D, E]>;
  419. /**
  420. * Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected.
  421. */
  422. export function all<A, B, C, D>(
  423. promises: IWhenable<[IWhenable<A>, IWhenable<B>, IWhenable<C>, IWhenable<D>]>,
  424. ): Promise<[A, B, C, D]>;
  425. /**
  426. * Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected.
  427. */
  428. export function all<A, B, C>(promises: IWhenable<[IWhenable<A>, IWhenable<B>, IWhenable<C>]>): Promise<[A, B, C]>;
  429. /**
  430. * Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected.
  431. */
  432. export function all<A, B>(promises: IWhenable<[IPromise<A>, IPromise<B>]>): Promise<[A, B]>;
  433. export function all<A, B>(promises: IWhenable<[A, IPromise<B>]>): Promise<[A, B]>;
  434. export function all<A, B>(promises: IWhenable<[IPromise<A>, B]>): Promise<[A, B]>;
  435. export function all<A, B>(promises: IWhenable<[A, B]>): Promise<[A, B]>;
  436. /**
  437. * Returns a promise that is fulfilled with an array containing the fulfillment value of each promise, or is rejected with the same rejection reason as the first promise to be rejected.
  438. */
  439. export function all<T>(promises: IWhenable<Array<IWhenable<T>>>): Promise<T[]>;
  440. /**
  441. * Returns a promise for the first of an array of promises to become settled.
  442. */
  443. export function race<T>(promises: Array<IWhenable<T>>): Promise<T>;
  444. /**
  445. * Returns a promise that is fulfilled with an array of promise state snapshots, but only after all the original promises
  446. * have settled, i.e. become either fulfilled or rejected.
  447. */
  448. export function allSettled<T>(promises: IWhenable<Array<IWhenable<T>>>): Promise<Array<PromiseState<T>>>;
  449. /**
  450. * Deprecated Alias for allSettled()
  451. */
  452. export function allResolved<T>(promises: IWhenable<Array<IWhenable<T>>>): Promise<Array<Promise<T>>>;
  453. /**
  454. * Like then, but "spreads" the array into a variadic fulfillment handler. If any of the promises in the array are
  455. * rejected, instead calls onRejected with the first rejected promise's rejection reason. This is especially useful
  456. * in conjunction with all.
  457. */
  458. export function spread<T, U>(
  459. promises: Array<IWhenable<T>>,
  460. onFulfilled: (...args: T[]) => IWhenable<U>,
  461. onRejected?: (reason: any) => IWhenable<U>,
  462. ): Promise<U>;
  463. /**
  464. * Returns a promise that will have the same result as promise, except that if promise is not fulfilled or rejected
  465. * before ms milliseconds, the returned promise will be rejected with an Error with the given message. If message
  466. * is not supplied, the message will be "Timed out after " + ms + " ms".
  467. */
  468. export function timeout<T>(promise: Promise<T>, ms: number, message?: string): Promise<T>;
  469. /**
  470. * Returns a promise that will have the same result as promise, but will only be fulfilled or rejected after at least ms milliseconds have passed.
  471. */
  472. export function delay<T>(promiseOrValue: Promise<T> | T, ms: number): Promise<T>;
  473. /**
  474. * Returns a promise that will be fulfilled with undefined after at least ms milliseconds have passed.
  475. */
  476. export function delay(ms: number): Promise<void>;
  477. /**
  478. * Returns whether a given promise is in the fulfilled state. When the static version is used on non-promises, the result is always true.
  479. */
  480. export function isFulfilled(promise: Promise<any>): boolean;
  481. /**
  482. * Returns whether a given promise is in the rejected state. When the static version is used on non-promises, the result is always false.
  483. */
  484. export function isRejected(promise: Promise<any>): boolean;
  485. /**
  486. * Returns whether a given promise is in the pending state. When the static version is used on non-promises, the result is always false.
  487. */
  488. export function isPending(promiseOrObject: Promise<any> | any): boolean;
  489. /**
  490. * Synchronously calls resolver(resolve, reject, notify) and returns a promise whose state is controlled by the
  491. * functions passed to resolver. This is an alternative promise-creation API that has the same power as the deferred
  492. * concept, but without introducing another conceptual entity.
  493. * If resolver throws an exception, the returned promise will be rejected with that thrown exception as the rejection reason.
  494. * note: In the latest github, this method is called Q.Promise, but if you are using the npm package version 0.9.7
  495. * or below, the method is called Q.promise (lowercase vs uppercase p).
  496. */
  497. export function Promise<T>(
  498. resolver: (
  499. resolve: (val?: IWhenable<T>) => void,
  500. reject: (reason?: any) => void,
  501. notify: (progress: any) => void,
  502. ) => void,
  503. ): Promise<T>;
  504. /**
  505. * Creates a new version of func that accepts any combination of promise and non-promise values, converting them to their
  506. * fulfillment values before calling the original func. The returned version also always returns a promise: if func does
  507. * a return or throw, then Q.promised(func) will return fulfilled or rejected promise, respectively.
  508. * This can be useful for creating functions that accept either promises or non-promise values, and for ensuring that
  509. * the function always returns a promise even in the face of unintentional thrown exceptions.
  510. */
  511. export function promised<T>(callback: (...args: any[]) => T): (...args: any[]) => Promise<T>;
  512. /**
  513. * Returns whether the given value is a Q promise.
  514. */
  515. export function isPromise(object: any): object is Promise<any>;
  516. /**
  517. * Returns whether the given value is a promise (i.e. it's an object with a then function).
  518. */
  519. export function isPromiseAlike(object: any): object is IPromise<any>;
  520. /**
  521. * If an object is not a promise, it is as "near" as possible.
  522. * If a promise is rejected, it is as "near" as possible too.
  523. * If it's a fulfilled promise, the fulfillment value is nearer.
  524. * If it's a deferred promise and the deferred has been resolved, the
  525. * resolution is "nearer".
  526. */
  527. export function nearer<T>(promise: Promise<T>): T;
  528. /**
  529. * This is an experimental tool for converting a generator function into a deferred function. This has the potential
  530. * of reducing nested callbacks in engines that support yield.
  531. */
  532. export function async<T>(generatorFunction: any): (...args: any[]) => Promise<T>;
  533. export function nextTick(callback: (...args: any[]) => any): void;
  534. /**
  535. * A settable property that will intercept any uncaught errors that would otherwise be thrown in the next tick of the
  536. * event loop, usually as a result of done. Can be useful for getting the full
  537. * stack trace of an error in browsers, which is not usually possible with window.onerror.
  538. */
  539. export let onerror: (reason: any) => void;
  540. /**
  541. * A settable property that lets you turn on long stack trace support. If turned on, "stack jumps" will be tracked
  542. * across asynchronous promise operations, so that if an uncaught error is thrown by done or a rejection reason's stack
  543. * property is inspected in a rejection callback, a long stack trace is produced.
  544. */
  545. export let longStackSupport: boolean;
  546. /**
  547. * Resets the global "Q" variable to the value it has before Q was loaded.
  548. * This will either be undefined if there was no version or the version of Q which was already loaded before.
  549. * @returns The last version of Q.
  550. */
  551. export function noConflict(): typeof Q;
  552. }