1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | declare function Q<T>(promise: Q.IPromise<T>): Q.Promise<T>;
|
11 |
|
12 |
|
13 |
|
14 | declare function Q<T>(value: T): Q.Promise<T>;
|
15 |
|
16 | declare 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 |
|
32 |
|
33 |
|
34 |
|
35 | fin(finallyCallback: () => any): Promise<T>;
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | finally(finallyCallback: () => any): Promise<T>;
|
42 |
|
43 | |
44 |
|
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 |
|
201 |
|
202 | export function all<T>(promises: IPromise<T>[]): Promise<T[]>;
|
203 |
|
204 | |
205 |
|
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 |
|
213 |
|
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 |
|
219 |
|
220 | export function timeout<T>(promise: Promise<T>, ms: number, message?: string): Promise<T>;
|
221 |
|
222 | |
223 |
|
224 |
|
225 | export function delay<T>(promise: Promise<T>, ms: number): Promise<T>;
|
226 | |
227 |
|
228 |
|
229 | export function delay<T>(value: T, ms: number): Promise<T>;
|
230 | |
231 |
|
232 |
|
233 | export function delay(ms: number): Promise <void>;
|
234 | |
235 |
|
236 |
|
237 | export function isFulfilled(promise: Promise<any>): boolean;
|
238 | |
239 |
|
240 |
|
241 | export function isRejected(promise: Promise<any>): boolean;
|
242 | |
243 |
|
244 |
|
245 | export function isPending(promise: Promise<any>): boolean;
|
246 |
|
247 | |
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | export function defer<T>(): Deferred<T>;
|
256 |
|
257 | |
258 |
|
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 |
|
266 |
|
267 |
|
268 |
|
269 | export function promised<T>(callback: (...args: any[]) => T): (...args: any[]) => Promise<T>;
|
270 |
|
271 | |
272 |
|
273 |
|
274 | export function isPromise(object: any): boolean;
|
275 | |
276 |
|
277 |
|
278 | export function isPromiseAlike(object: any): boolean;
|
279 | |
280 |
|
281 |
|
282 | export function isPending(object: any): boolean;
|
283 |
|
284 | |
285 |
|
286 |
|
287 | export function async<T>(generatorFunction: any): (...args: any[]) => Promise<T>;
|
288 | export function nextTick(callback: Function): void;
|
289 |
|
290 | |
291 |
|
292 |
|
293 | export var onerror: (reason: any) => void;
|
294 | |
295 |
|
296 |
|
297 | export var longStackSupport: boolean;
|
298 |
|
299 | |
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 | export function resolve<T>(object: IPromise<T>): Promise<T>;
|
306 | |
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 | export function resolve<T>(object: T): Promise<T>;
|
313 | }
|
314 |
|
315 | declare module "q" {
|
316 | export = Q;
|
317 | } |
\ | No newline at end of file |