* 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 arra
(array, fn, self = undefined)
| 164 | * @template TYPE, SELF |
| 165 | */ |
| 166 | async function filter(array, fn, self = undefined) { |
| 167 | const v = await Promise.resolve(array) |
| 168 | if (!Array.isArray(v)) { |
| 169 | throw TypeError('not an array') |
| 170 | } |
| 171 | |
| 172 | const arr = /** @type {!Array} */ (v) |
| 173 | const values = [] |
| 174 | |
| 175 | for (const [index, item] of arr.entries()) { |
| 176 | const isConditionTrue = await Promise.resolve(fn.call(self, item, index, arr)) |
| 177 | if (isConditionTrue) { |
| 178 | values.push(item) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | return values |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Returns a promise that will be resolved with the input value in a |
no test coverage detected