2

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?

3
  • is this unclear? why people put -1? dude, I just need some advice, thats all Commented Oct 28, 2020 at 14:26
  • Have you tried typing it as void? Commented Oct 28, 2020 at 14:28
  • yup, the same tslint message Commented Oct 28, 2020 at 14:40

2 Answers 2

4

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)); } 
Sign up to request clarification or add additional context in comments.

2 Comments

yup, ur right, same as above, where I can learn about generics? every page has complex explanations, and Its hard for me trying to understand them
The basics are that it's an [thing] of [another thing]. e.g, "Array<string>" is an "array of strings". Generics let you describe how the type works in general (ie, they let you describe what "of" means in "array of strings"), and then when you use it, you can be more specific. In the case of a promise, the thing you get to be more specific about is what the promise will resolve to. Promise<string> is a promise that resolves to a string; Promise<void> is a promise that resolves to void
1

The return type should be Promise<void>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.