You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.d.ts 28KB

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