-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
promise.js
266 lines (241 loc) · 8.31 KB
/
promise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Licensed to the Software Freedom Conservancy (SFC) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The SFC licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
/**
* @fileoverview Defines a handful of utility functions to simplify working
* with promises.
*/
'use strict'
const { isObject, isPromise } = require('./util')
/**
* Creates a promise that will be resolved at a set time in the future.
* @param {number} ms The amount of time, in milliseconds, to wait before
* resolving the promise.
* @return {!Promise<void>} The promise.
*/
function delayed(ms) {
return new Promise((resolve) => setTimeout(resolve, ms))
}
/**
* Wraps a function that expects a node-style callback as its final
* argument. This callback expects two arguments: an error value (which will be
* null if the call succeeded), and the success value as the second argument.
* The callback will the resolve or reject the returned promise, based on its
* arguments.
* @param {!Function} fn The function to wrap.
* @param {...?} args The arguments to apply to the function, excluding the
* final callback.
* @return {!Thenable} A promise that will be resolved with the
* result of the provided function's callback.
*/
function checkedNodeCall(fn, ...args) {
return new Promise(function (fulfill, reject) {
try {
fn(...args, function (error, value) {
error ? reject(error) : fulfill(value)
})
} catch (ex) {
reject(ex)
}
})
}
/**
* Registers a listener to invoke when a promise is resolved, regardless
* of whether the promise's value was successfully computed. This function
* is synonymous with the {@code finally} clause in a synchronous API:
*
* // Synchronous API:
* try {
* doSynchronousWork();
* } finally {
* cleanUp();
* }
*
* // Asynchronous promise API:
* doAsynchronousWork().finally(cleanUp);
*
* __Note:__ similar to the {@code finally} clause, if the registered
* callback returns a rejected promise or throws an error, it will silently
* replace the rejection error (if any) from this promise:
*
* try {
* throw Error('one');
* } finally {
* throw Error('two'); // Hides Error: one
* }
*
* let p = Promise.reject(Error('one'));
* promise.finally(p, function() {
* throw Error('two'); // Hides Error: one
* });
*
* @param {!IThenable<?>} promise The promise to add the listener to.
* @param {function(): (R|IThenable<R>)} callback The function to call when
* the promise is resolved.
* @return {!Promise<R>} A promise that will be resolved with the callback
* result.
* @template R
*/
async function thenFinally(promise, callback) {
try {
await Promise.resolve(promise)
return callback()
} catch (e) {
await callback()
throw e
}
}
/**
* Calls a function for each element in an array and inserts the result into a
* new array, which is used as the fulfillment value of the promise returned
* by this function.
*
* If the return value of the mapping function is a promise, this function
* will wait for it to be fulfilled before inserting it into the new array.
*
* If the mapping function throws or returns a rejected promise, the
* promise returned by this function will be rejected with the same reason.
* Only the first failure will be reported; all subsequent errors will be
* silently ignored.
*
* @param {!(Array<TYPE>|IThenable<!Array<TYPE>>)} array The array to iterate
* over, or a promise that will resolve to said array.
* @param {function(this: SELF, TYPE, number, !Array<TYPE>): ?} fn The
* function to call for each element in the array. This function should
* expect three arguments (the element, the index, and the array itself.
* @param {SELF=} self The object to be used as the value of 'this' within `fn`.
* @template TYPE, SELF
*/
async function map(array, fn, self = undefined) {
const v = await Promise.resolve(array)
if (!Array.isArray(v)) {
throw TypeError('not an array')
}
const arr = /** @type {!Array} */ (v)
const values = []
for (const [index, item] of arr.entries()) {
values.push(await Promise.resolve(fn.call(self, item, index, arr)))
}
return values
}
/**
* Calls a function for each element in an array, and if the function returns
* true adds the element to a new array.
*
* If the return value of the filter function is a promise, this function
* will wait for it to be fulfilled before determining whether to insert the
* element into the new array.
*
* If the filter function throws or returns a rejected promise, the promise
* returned by this function will be rejected with the same reason. Only the
* first failure will be reported; all subsequent errors will be silently
* ignored.
*
* @param {!(Array<TYPE>|IThenable<!Array<TYPE>>)} array The array to iterate
* over, or a promise that will resolve to said array.
* @param {function(this: SELF, TYPE, number, !Array<TYPE>): (
* boolean|IThenable<boolean>)} fn The function
* to call for each element in the array.
* @param {SELF=} self The object to be used as the value of 'this' within `fn`.
* @template TYPE, SELF
*/
async function filter(array, fn, self = undefined) {
const v = await Promise.resolve(array)
if (!Array.isArray(v)) {
throw TypeError('not an array')
}
const arr = /** @type {!Array} */ (v)
const values = []
for (const [index, item] of arr.entries()) {
const isConditionTrue = await Promise.resolve(fn.call(self, item, index, arr))
if (isConditionTrue) {
values.push(item)
}
}
return values
}
/**
* Returns a promise that will be resolved with the input value in a
* fully-resolved state. If the value is an array, each element will be fully
* resolved. Likewise, if the value is an object, all keys will be fully
* resolved. In both cases, all nested arrays and objects will also be
* fully resolved. All fields are resolved in place; the returned promise will
* resolve on {@code value} and not a copy.
*
* Warning: This function makes no checks against objects that contain
* cyclical references:
*
* var value = {};
* value['self'] = value;
* promise.fullyResolved(value); // Stack overflow.
*
* @param {*} value The value to fully resolve.
* @return {!Thenable} A promise for a fully resolved version
* of the input value.
*/
async function fullyResolved(value) {
value = await Promise.resolve(value)
if (Array.isArray(value)) {
return fullyResolveKeys(/** @type {!Array} */ (value))
}
if (isObject(value)) {
return fullyResolveKeys(/** @type {!Object} */ (value))
}
if (typeof value === 'function') {
return fullyResolveKeys(/** @type {!Object} */ (value))
}
return value
}
/**
* @param {!(Array|Object)} obj the object to resolve.
* @return {!Thenable} A promise that will be resolved with the
* input object once all of its values have been fully resolved.
*/
async function fullyResolveKeys(obj) {
const isArray = Array.isArray(obj)
const numKeys = isArray ? obj.length : Object.keys(obj).length
if (!numKeys) {
return obj
}
async function forEachProperty(obj, fn) {
for (let key in obj) {
await fn(obj[key], key)
}
}
async function forEachElement(arr, fn) {
for (let i = 0; i < arr.length; i++) {
await fn(arr[i], i)
}
}
const forEachKey = isArray ? forEachElement : forEachProperty
await forEachKey(obj, async function (partialValue, key) {
if (!Array.isArray(partialValue) && (!partialValue || typeof partialValue !== 'object')) {
return
}
obj[key] = await fullyResolved(partialValue)
})
return obj
}
// PUBLIC API
module.exports = {
checkedNodeCall,
delayed,
filter,
finally: thenFinally,
fullyResolved,
isPromise,
map,
}