Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

typescript returntype remove promise

type UnPromisifiedObject<T> = {[k in keyof T]: UnPromisify<T[k]>}
type UnPromisify<T> = T extends Promise<infer U> ? U : T;

async function promise_props<T extends {[key: string]: Promise<any>}>(obj: T): Promise<UnPromisifiedObject<T>> {
    const keys = Object.keys(obj);
    const awaitables = keys.map(key => obj[key]);

    const values = await Promise.all(awaitables);
    const result = {} as any;

    keys.forEach((key, i) => {
        result[key] = values[i];
    });
    return result as UnPromisifiedObject<T>;
}

async function main() {
    const x = {
        company: Promise.resolve("company"),
        page: Promise.resolve(1)
    };
    const res = await promise_props(x);
    const company = res.company;  // company is a string here
    const page = res.page;        // page is a number here
}
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript type or null 
Typescript :: material form 
Typescript :: typescript generic object 
Typescript :: select constraints in sql 
Typescript :: github sync local with remote 
Typescript :: draw image html canvas 
Typescript :: basic variable typescript 
Typescript :: typescript check if object is of type 
Typescript :: conditional styled components with media query 
Typescript :: typescript err type 
Typescript :: typescript value in enum 
Typescript :: comments visual studio code html 
Typescript :: verify if object is of a certain type type in typescript 
Typescript :: angular initail valeur in fromgroup 
Typescript :: createasyncthunk with typescript 
Typescript :: how to set date axes limits in matplotlib plot 
Typescript :: update behaviorsubject value without emitting 
Typescript :: c# get all elements from list 
Typescript :: persists meaning 
Typescript :: typescript get objects nested in object 
Typescript :: how to parameterize test cases 
Typescript :: how to compare two date in typescript 
Typescript :: __REDUX_DEVTOOLS_EXTENSION_COMPOSE__ 
Typescript :: custom events in unity c# 
Typescript :: is missing in props validation typescript 
Typescript :: typescript generic type 
Typescript :: props react typescript 
Typescript :: typescript named return 
Typescript :: rust typedef 
Typescript :: typescript `is a` function determine type 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =