( list: T[], resolver: (val: T, index: number, values: T[]) => ValueOrPromise<V>, )
| 155 | * be invoked with the property value, the property index, and the source array. |
| 156 | */ |
| 157 | export function resolveList<T, V>( |
| 158 | list: T[], |
| 159 | resolver: (val: T, index: number, values: T[]) => ValueOrPromise<V>, |
| 160 | ): ValueOrPromise<V[]> { |
| 161 | const result: V[] = new Array<V>(list.length); |
| 162 | let asyncResolvers: PromiseLike<void>[] | undefined = undefined; |
| 163 | |
| 164 | const setter = (index: number) => (val: V) => { |
| 165 | result[index] = val; |
| 166 | }; |
| 167 | |
| 168 | for (let ix = 0; ix < list.length; ix++) { |
| 169 | const valueOrPromise = resolver(list[ix], ix, list); |
| 170 | if (isPromiseLike(valueOrPromise)) { |
| 171 | if (!asyncResolvers) asyncResolvers = []; |
| 172 | asyncResolvers.push(valueOrPromise.then(setter(ix))); |
| 173 | } else { |
| 174 | result[ix] = valueOrPromise; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | if (asyncResolvers) { |
| 179 | return Promise.all(asyncResolvers).then(() => result); |
| 180 | } else { |
| 181 | return result; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Try to run an action that returns a promise or a value |
no test coverage detected