Skip to content

resolveProps() function

解析对象中所有可能是 promise 的属性。

Signature:

typescript
declare function resolveProps<T extends object, Key = Exclude<keyof T, symbol>>(
  obj: T,
): Promise<{
  [K in keyof T]: K extends Key ? Awaited<T[K]> : never;
}>;

Example

ts
const obj = {
  a: Promise.resolve(1),
  b: 2,
  c: Promise.resolve(3),
};

const result = await resolveProps(obj);
console.log(result); // { a: 1, b: 2, c: 3 }

Also, with this function, you can define asyncEvolve simply.

ts
const asyncEvolve =
  <T extends object>(transformation: Transformation<T>) =>
  (obj: T) =>
    pipe(obj, evolve(transformation), resolveProps);

const a = await asyncEvolve({ a: async (a) => a + 1 })({ a: 1 });
console.log(a); // { a: 2 }

const resultByFormat = await asyncEvolve<Request>({
  foo: async (foo) => await fetch(`https://example.com/foo/${foo}`).json(),
  bar: async (bar) => await fetch(`https://example.com/bar/${bar}`).text(),
});
const result = await resultByFormat({ foo: "foo", bar: "bar" });

console.log({
  foo: result.foo,
  bar: result.bar,
});

see pipe and evolve.

Open Source Code

Released under the Apache-2.0 License.