How to type async function in TypeScript when its a void function

Viewed 2439

Here is my code:

async buildSomething(): any {
    const requestData = await request;
    requestData.forEach(i => this.table.push(i));
}

How I should type a void function, because it does something but It does not return anything.

In my case I used any but tslint shows me this:

Type 'any' is not a valid async function return type in ES5/ES3 because it does not refer to a Promise-compatible constructor value.

How should I achieve this?

2 Answers

All async functions return something: they return promises. So you don't want void, you want Promise<void>

async buildSomething(): Promise<void> {
    const requestData = await request;
    requestData.forEach(i => this.table.push(i));
}

The return type should be Promise<void>

Related