1 | import { EventEmitter } from '../';
|
2 |
|
3 | /**
|
4 | * promise
|
5 | */
|
6 |
|
7 | export namespace promise {
|
8 | // region Functions
|
9 |
|
10 | /**
|
11 | * Determines whether a {@code value} should be treated as a promise.
|
12 | * Any object whose 'then' property is a function will be considered a
|
13 | * promise.
|
14 | */
|
15 | function isPromise(value: any): boolean;
|
16 |
|
17 | /**
|
18 | * Creates a promise that will be resolved at a set time in the future.
|
19 | */
|
20 | function delayed(ms: number): Promise<void>;
|
21 |
|
22 | /**
|
23 | * Calls a function for each element in an array, and if the function returns
|
24 | * true adds the element to a new array.
|
25 | *
|
26 | * If the return value of the filter function is a promise, this function
|
27 | * will wait for it to be fulfilled before determining whether to insert the
|
28 | * element into the new array.
|
29 | *
|
30 | * If the filter function throws or returns a rejected promise, the promise
|
31 | * returned by this function will be rejected with the same reason. Only the
|
32 | * first failure will be reported; all subsequent errors will be silently
|
33 | * ignored.
|
34 | */
|
35 | function filter<T, V>(
|
36 | arr: T[]|Promise<T[]>, fn: (element: T, index: number, array: T[]) => V,
|
37 | // value
|
38 | optSelf?: any): Promise<V[]>;
|
39 |
|
40 | /**
|
41 | * Calls a function for each element in an array and inserts the result into a
|
42 | * new array, which is used as the fulfillment value of the promise returned
|
43 | * by this function.
|
44 | *
|
45 | * If the return value of the mapping function is a promise, this function
|
46 | * will wait for it to be fulfilled before inserting it into the new array.
|
47 | *
|
48 | * If the mapping function throws or returns a rejected promise, the
|
49 | * promise returned by this function will be rejected with the same reason.
|
50 | * Only the first failure will be reported; all subsequent errors will be
|
51 | * silently ignored.
|
52 | */
|
53 | function map<T, V>(
|
54 | arr: T[]|Promise<T[]>, fn: (self: any, type: T, index: number, array: T[]) => V,
|
55 | optSelf?: any): Promise<V[]>;
|
56 |
|
57 | /**
|
58 | * Wraps a function that expects a node-style callback as its final
|
59 | * argument. This callback expects two arguments: an error value (which will
|
60 | * be null if the call succeeded), and the success value as the second
|
61 | * argument. The callback will the resolve or reject the returned promise,
|
62 | * based on its arguments.
|
63 | */
|
64 | function checkedNodeCall<T>(fn: Function, ...varArgs: any[]): Promise<T>;
|
65 |
|
66 | /**
|
67 | * Returns a promise that will be resolved with the input value in a
|
68 | * fully-resolved state. If the value is an array, each element will be fully
|
69 | * resolved. Likewise, if the value is an object, all keys will be fully
|
70 | * resolved. In both cases, all nested arrays and objects will also be
|
71 | * fully resolved. All fields are resolved in place; the returned promise
|
72 | * will resolve on {@code value} and not a copy.
|
73 | *
|
74 | * Warning: This function makes no checks against objects that contain
|
75 | * cyclical references:
|
76 | *
|
77 | * var value = {};
|
78 | * value['self'] = value;
|
79 | * promise.fullyResolved(value); // Stack overflow.
|
80 | */
|
81 | function fullyResolved(value: any): Promise<any>;
|
82 |
|
83 | /**
|
84 | * Registers a listener to invoke when a promise is resolved, regardless
|
85 | * of whether the promise's value was successfully computed. This function
|
86 | * is synonymous with the {@code finally} clause in a synchronous API:
|
87 | *
|
88 | * // Synchronous API:
|
89 | * try {
|
90 | * doSynchronousWork();
|
91 | * } finally {
|
92 | * cleanUp();
|
93 | * }
|
94 | *
|
95 | * // Asynchronous promise API:
|
96 | * doAsynchronousWork().finally(cleanUp);
|
97 | *
|
98 | * __Note:__ similar to the {@code finally} clause, if the registered
|
99 | * callback returns a rejected promise or throws an error, it will silently
|
100 | * replace the rejection error (if any) from this promise:
|
101 | *
|
102 | * try {
|
103 | * throw Error('one');
|
104 | * } finally {
|
105 | * throw Error('two'); // Hides Error: one
|
106 | * }
|
107 | *
|
108 | * let p = Promise.reject(Error('one'));
|
109 | * promise.finally(p, function() {
|
110 | * throw Error('two'); // Hides Error: one
|
111 | * });
|
112 | */
|
113 | function thenFinally<R>(promise: any, callback: () => R | Promise<R>): Promise<R>;
|
114 |
|
115 | // endregion
|
116 | }
|