UNPKG

16.9 kBTypeScriptView Raw
1// Type definitions for Q
2// Project: https://github.com/kriskowal/q
3// Definitions by: Barrie Nemetchek <https://github.com/bnemetchek>, Andrew Gaspar <https://github.com/AndrewGaspar/>, John Reilly <https://github.com/johnnyreilly>
4// Definitions: https://github.com/borisyankov/DefinitelyTyped
5
6/**
7 * If value is a Q promise, returns the promise.
8 * If value is a promise from another library it is coerced into a Q promise (where possible).
9 */
10declare function Q<T>(promise: Q.IPromise<T>): Q.Promise<T>;
11/**
12 * If value is not a promise, returns a promise that is fulfilled with value.
13 */
14declare function Q<T>(value: T): Q.Promise<T>;
15
16declare module Q {
17 interface IPromise<T> {
18 then<U>(onFulfill?: (value: T) => U | IPromise<U>, onReject?: (error: any) => U | IPromise<U>): IPromise<U>;
19 }
20
21 interface Deferred<T> {
22 promise: Promise<T>;
23 resolve(value: T): void;
24 reject(reason: any): void;
25 notify(value: any): void;
26 makeNodeResolver(): (reason: any, value: T) => void;
27 }
28
29 interface Promise<T> {
30 /**
31 * Like a finally clause, allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful for collecting resources regardless of whether a job succeeded, like closing a database connection, shutting a server down, or deleting an unneeded key from an object.
32
33 * finally returns a promise, which will become resolved with the same fulfillment value or rejection reason as promise. However, if callback returns a promise, the resolution of the returned promise will be delayed until the promise returned from callback is finished.
34 */
35 fin(finallyCallback: () => any): Promise<T>;
36 /**
37 * Like a finally clause, allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful for collecting resources regardless of whether a job succeeded, like closing a database connection, shutting a server down, or deleting an unneeded key from an object.
38
39 * finally returns a promise, which will become resolved with the same fulfillment value or rejection reason as promise. However, if callback returns a promise, the resolution of the returned promise will be delayed until the promise returned from callback is finished.
40 */
41 finally(finallyCallback: () => any): Promise<T>;
42
43 /**
44 * The then method from the Promises/A+ specification, with an additional progress handler.
45 */
46 then<U>(onFulfill?: (value: T) => U | IPromise<U>, onReject?: (error: any) => U | IPromise<U>, onProgress?: Function): Promise<U>;
47
48 /**
49 * Like then, but "spreads" the array into a variadic fulfillment handler. If any of the promises in the array are rejected, instead calls onRejected with the first rejected promise's rejection reason.
50 *
51 * This is especially useful in conjunction with all
52 */
53 spread<U>(onFulfilled: Function, onRejected?: Function): Promise<U>;
54
55 fail<U>(onRejected: (reason: any) => U | IPromise<U>): Promise<U>;
56
57 /**
58 * A sugar method, equivalent to promise.then(undefined, onRejected).
59 */
60 catch<U>(onRejected: (reason: any) => U | IPromise<U>): Promise<U>;
61
62 /**
63 * A sugar method, equivalent to promise.then(undefined, undefined, onProgress).
64 */
65 progress(onProgress: (progress: any) => any): Promise<T>;
66
67 /**
68 * Much like then, but with different behavior around unhandled rejection. If there is an unhandled rejection, either because promise is rejected and no onRejected callback was provided, or because onFulfilled or onRejected threw an error or returned a rejected promise, the resulting rejection reason is thrown as an exception in a future turn of the event loop.
69 *
70 * This method should be used to terminate chains of promises that will not be passed elsewhere. Since exceptions thrown in then callbacks are consumed and transformed into rejections, exceptions at the end of the chain are easy to accidentally, silently ignore. By arranging for the exception to be thrown in a future turn of the event loop, so that it won't be caught, it causes an onerror event on the browser window, or an uncaughtException event on Node.js's process object.
71 *
72 * Exceptions thrown by done will have long stack traces, if Q.longStackSupport is set to true. If Q.onerror is set, exceptions will be delivered there instead of thrown in a future turn.
73 *
74 * The Golden Rule of done vs. then usage is: either return your promise to someone else, or if the chain ends with you, call done to terminate it.
75 */
76 done(onFulfilled?: (value: T) => any, onRejected?: (reason: any) => any, onProgress?: (progress: any) => any): void;
77
78 /**
79 * If callback is a function, assumes it's a Node.js-style callback, and calls it as either callback(rejectionReason) when/if promise becomes rejected, or as callback(null, fulfillmentValue) when/if promise becomes fulfilled. If callback is not a function, simply returns promise.
80 */
81 nodeify(callback: (reason: any, value: any) => void): Promise<T>;
82
83 /**
84 * Returns a promise to get the named property of an object. Essentially equivalent to
85 *
86 * promise.then(function (o) {
87 * return o[propertyName];
88 * });
89 */
90 get<U>(propertyName: String): Promise<U>;
91 set<U>(propertyName: String, value: any): Promise<U>;
92 delete<U>(propertyName: String): Promise<U>;
93 /**
94 * Returns a promise for the result of calling the named method of an object with the given array of arguments. The object itself is this in the function, just like a synchronous method call. Essentially equivalent to
95 *
96 * promise.then(function (o) {
97 * return o[methodName].apply(o, args);
98 * });
99 */
100 post<U>(methodName: String, args: any[]): Promise<U>;
101 /**
102 * Returns a promise for the result of calling the named method of an object with the given variadic arguments. The object itself is this in the function, just like a synchronous method call.
103 */
104 invoke<U>(methodName: String, ...args: any[]): Promise<U>;
105 fapply<U>(args: any[]): Promise<U>;
106 fcall<U>(...args: any[]): Promise<U>;
107
108 /**
109 * Returns a promise for an array of the property names of an object. Essentially equivalent to
110 *
111 * promise.then(function (o) {
112 * return Object.keys(o);
113 * });
114 */
115 keys(): Promise<string[]>;
116
117 /**
118 * A sugar method, equivalent to promise.then(function () { return value; }).
119 */
120 thenResolve<U>(value: U): Promise<U>;
121 /**
122 * A sugar method, equivalent to promise.then(function () { throw reason; }).
123 */
124 thenReject(reason: any): Promise<T>;
125 timeout(ms: number, message?: string): Promise<T>;
126 /**
127 * 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.
128 */
129 delay(ms: number): Promise<T>;
130
131 /**
132 * Returns whether a given promise is in the fulfilled state. When the static version is used on non-promises, the result is always true.
133 */
134 isFulfilled(): boolean;
135 /**
136 * Returns whether a given promise is in the rejected state. When the static version is used on non-promises, the result is always false.
137 */
138 isRejected(): boolean;
139 /**
140 * Returns whether a given promise is in the pending state. When the static version is used on non-promises, the result is always false.
141 */
142 isPending(): boolean;
143
144 valueOf(): any;
145
146 /**
147 * Returns a "state snapshot" object, which will be in one of three forms:
148 *
149 * - { state: "pending" }
150 * - { state: "fulfilled", value: <fulfllment value> }
151 * - { state: "rejected", reason: <rejection reason> }
152 */
153 inspect(): PromiseState<T>;
154 }
155
156 interface PromiseState<T> {
157 /**
158 * "fulfilled", "rejected", "pending"
159 */
160 state: string;
161 value?: T;
162 reason?: any;
163 }
164
165 // If no value provided, returned promise will be of void type
166 export function when(): Promise<void>;
167
168 // if no fulfill, reject, or progress provided, returned promise will be of same type
169 export function when<T>(value: T | IPromise<T>): Promise<T>;
170
171 // If a non-promise value is provided, it will not reject or progress
172 export function when<T, U>(value: T | IPromise<T>, onFulfilled: (val: T) => U | IPromise<U>, onRejected?: (reason: any) => U | IPromise<U>, onProgress?: (progress: any) => any): Promise<U>;
173
174 /**
175 * Currently "impossible" (and I use the term loosely) to implement due to TypeScript limitations as it is now.
176 * See: https://github.com/Microsoft/TypeScript/issues/1784 for discussion on it.
177 */
178 // export function try(method: Function, ...args: any[]): Promise<any>;
179
180 export function fbind<T>(method: (...args: any[]) => T | IPromise<T>, ...args: any[]): (...args: any[]) => Promise<T>;
181
182 export function fcall<T>(method: (...args: any[]) => T, ...args: any[]): Promise<T>;
183
184 export function send<T>(obj: any, functionName: string, ...args: any[]): Promise<T>;
185 export function invoke<T>(obj: any, functionName: string, ...args: any[]): Promise<T>;
186 export function mcall<T>(obj: any, functionName: string, ...args: any[]): Promise<T>;
187
188 export function denodeify<T>(nodeFunction: Function, ...args: any[]): (...args: any[]) => Promise<T>;
189 export function nbind<T>(nodeFunction: Function, thisArg: any, ...args: any[]): (...args: any[]) => Promise<T>;
190 export function nfbind<T>(nodeFunction: Function, ...args: any[]): (...args: any[]) => Promise<T>;
191 export function nfcall<T>(nodeFunction: Function, ...args: any[]): Promise<T>;
192 export function nfapply<T>(nodeFunction: Function, args: any[]): Promise<T>;
193
194 export function ninvoke<T>(nodeModule: any, functionName: string, ...args: any[]): Promise<T>;
195 export function npost<T>(nodeModule: any, functionName: string, args: any[]): Promise<T>;
196 export function nsend<T>(nodeModule: any, functionName: string, ...args: any[]): Promise<T>;
197 export function nmcall<T>(nodeModule: any, functionName: string, ...args: any[]): Promise<T>;
198
199 /**
200 * 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.
201 */
202 export function all<T>(promises: IPromise<T>[]): Promise<T[]>;
203
204 /**
205 * Returns a promise that is fulfilled with an array of promise state snapshots, but only after all the original promises have settled, i.e. become either fulfilled or rejected.
206 */
207 export function allSettled<T>(promises: IPromise<T>[]): Promise<PromiseState<T>[]>;
208
209 export function allResolved<T>(promises: IPromise<T>[]): Promise<Promise<T>[]>;
210
211 /**
212 * Like then, but "spreads" the array into a variadic fulfillment handler. If any of the promises in the array are rejected, instead calls onRejected with the first rejected promise's rejection reason.
213 * This is especially useful in conjunction with all.
214 */
215 export function spread<T, U>(promises: IPromise<T>[], onFulfilled: (...args: T[]) => U | IPromise<U>, onRejected?: (reason: any) => U | IPromise<U>): Promise<U>;
216
217 /**
218 * Returns a promise that will have the same result as promise, except that if promise is not fulfilled or rejected before ms milliseconds, the returned promise will be rejected with an Error with the given message. If message is not supplied, the message will be "Timed out after " + ms + " ms".
219 */
220 export function timeout<T>(promise: Promise<T>, ms: number, message?: string): Promise<T>;
221
222 /**
223 * 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.
224 */
225 export function delay<T>(promise: Promise<T>, ms: number): Promise<T>;
226 /**
227 * 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.
228 */
229 export function delay<T>(value: T, ms: number): Promise<T>;
230 /**
231 * Returns a promise that will be fulfilled with undefined after at least ms milliseconds have passed.
232 */
233 export function delay(ms: number): Promise <void>;
234 /**
235 * Returns whether a given promise is in the fulfilled state. When the static version is used on non-promises, the result is always true.
236 */
237 export function isFulfilled(promise: Promise<any>): boolean;
238 /**
239 * Returns whether a given promise is in the rejected state. When the static version is used on non-promises, the result is always false.
240 */
241 export function isRejected(promise: Promise<any>): boolean;
242 /**
243 * Returns whether a given promise is in the pending state. When the static version is used on non-promises, the result is always false.
244 */
245 export function isPending(promise: Promise<any>): boolean;
246
247 /**
248 * Returns a "deferred" object with a:
249 * promise property
250 * resolve(value) method
251 * reject(reason) method
252 * notify(value) method
253 * makeNodeResolver() method
254 */
255 export function defer<T>(): Deferred<T>;
256
257 /**
258 * Returns a promise that is rejected with reason.
259 */
260 export function reject<T>(reason?: any): Promise<T>;
261
262 export function Promise<T>(resolver: (resolve: (val: T | IPromise<T>) => void , reject: (reason: any) => void , notify: (progress: any) => void ) => void ): Promise<T>;
263
264 /**
265 * Creates a new version of func that accepts any combination of promise and non-promise values, converting them to their fulfillment values before calling the original func. The returned version also always returns a promise: if func does a return or throw, then Q.promised(func) will return fulfilled or rejected promise, respectively.
266 *
267 * This can be useful for creating functions that accept either promises or non-promise values, and for ensuring that the function always returns a promise even in the face of unintentional thrown exceptions.
268 */
269 export function promised<T>(callback: (...args: any[]) => T): (...args: any[]) => Promise<T>;
270
271 /**
272 * Returns whether the given value is a Q promise.
273 */
274 export function isPromise(object: any): boolean;
275 /**
276 * Returns whether the given value is a promise (i.e. it's an object with a then function).
277 */
278 export function isPromiseAlike(object: any): boolean;
279 /**
280 * Returns whether a given promise is in the pending state. When the static version is used on non-promises, the result is always false.
281 */
282 export function isPending(object: any): boolean;
283
284 /**
285 * This is an experimental tool for converting a generator function into a deferred function. This has the potential of reducing nested callbacks in engines that support yield.
286 */
287 export function async<T>(generatorFunction: any): (...args: any[]) => Promise<T>;
288 export function nextTick(callback: Function): void;
289
290 /**
291 * A settable property that will intercept any uncaught errors that would otherwise be thrown in the next tick of the event loop, usually as a result of done. Can be useful for getting the full stack trace of an error in browsers, which is not usually possible with window.onerror.
292 */
293 export var onerror: (reason: any) => void;
294 /**
295 * A settable property that lets you turn on long stack trace support. If turned on, "stack jumps" will be tracked across asynchronous promise operations, so that if an uncaught error is thrown by done or a rejection reason's stack property is inspected in a rejection callback, a long stack trace is produced.
296 */
297 export var longStackSupport: boolean;
298
299 /**
300 * Calling resolve with a pending promise causes promise to wait on the passed promise, becoming fulfilled with its fulfillment value or rejected with its rejection reason (or staying pending forever, if the passed promise does).
301 * Calling resolve with a rejected promise causes promise to be rejected with the passed promise's rejection reason.
302 * Calling resolve with a fulfilled promise causes promise to be fulfilled with the passed promise's fulfillment value.
303 * Calling resolve with a non-promise value causes promise to be fulfilled with that value.
304 */
305 export function resolve<T>(object: IPromise<T>): Promise<T>;
306 /**
307 * Calling resolve with a pending promise causes promise to wait on the passed promise, becoming fulfilled with its fulfillment value or rejected with its rejection reason (or staying pending forever, if the passed promise does).
308 * Calling resolve with a rejected promise causes promise to be rejected with the passed promise's rejection reason.
309 * Calling resolve with a fulfilled promise causes promise to be fulfilled with the passed promise's fulfillment value.
310 * Calling resolve with a non-promise value causes promise to be fulfilled with that value.
311 */
312 export function resolve<T>(object: T): Promise<T>;
313}
314
315declare module "q" {
316 export = Q;
317}
\No newline at end of file