typescript returntype remove promise

Solutions on MaxInterview for typescript returntype remove promise by the best coders in the world

showing results for - "typescript returntype remove promise"
Gretchen
22 Feb 2017
1type UnPromisifiedObject<T> = {[k in keyof T]: UnPromisify<T[k]>}
2type UnPromisify<T> = T extends Promise<infer U> ? U : T;
3
4async function promise_props<T extends {[key: string]: Promise<any>}>(obj: T): Promise<UnPromisifiedObject<T>> {
5    const keys = Object.keys(obj);
6    const awaitables = keys.map(key => obj[key]);
7
8    const values = await Promise.all(awaitables);
9    const result = {} as any;
10
11    keys.forEach((key, i) => {
12        result[key] = values[i];
13    });
14    return result as UnPromisifiedObject<T>;
15}
16
17async function main() {
18    const x = {
19        company: Promise.resolve("company"),
20        page: Promise.resolve(1)
21    };
22    const res = await promise_props(x);
23    const company = res.company;  // company is a string here
24    const page = res.page;        // page is a number here
25}
26
similar questions
queries leading to this page
typescript returntype remove promise