* 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 ins
(array, fn, self = undefined)
| 127 | * @template TYPE, SELF |
| 128 | */ |
| 129 | async function map(array, fn, self = undefined) { |
| 130 | const v = await Promise.resolve(array) |
| 131 | if (!Array.isArray(v)) { |
| 132 | throw TypeError('not an array') |
| 133 | } |
| 134 | |
| 135 | const arr = /** @type {!Array} */ (v) |
| 136 | const values = [] |
| 137 | |
| 138 | for (const [index, item] of arr.entries()) { |
| 139 | values.push(await Promise.resolve(fn.call(self, item, index, arr))) |
| 140 | } |
| 141 | |
| 142 | return values |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Calls a function for each element in an array, and if the function returns |