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