MCPcopy Index your code
hub / github.com/loopbackio/loopback-next / resolveList

Function resolveList

packages/context/src/value-promise.ts:157–183  ·  view source on GitHub ↗
(
  list: T[],
  resolver: (val: T, index: number, values: T[]) => ValueOrPromise<V>,
)

Source from the content-addressed store, hash-verified

155 * be invoked with the property value, the property index, and the source array.
156 */
157export 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

Callers 5

resolveInjectedArgumentsFunction · 0.90
resolveMethod · 0.90
greetFromAllFunction · 0.90
bootMethod · 0.85

Calls 3

isPromiseLikeFunction · 0.85
setterFunction · 0.70
resolverFunction · 0.50

Tested by

no test coverage detected