7

I am trying to understand why the return type string in the following method is underlined red as error:

exportPageAsText(pageNumber: number): string {
        (async () => {
            const text = await this.pdfViewerService.getPageAsText(pageNumber);
            console.log(text);
            return text;
        })();
}

The error message reads: A function whose declared type is neither 'void' nor 'any' must return a value. so I moved return text; out of the async scope and placed it after })(); but that made the text variable unrecognizable.

Then I thought maybe it's because the method return type should be a Promise so I changed the signature to:

exportPageAsText(pageNumber: number): Promise<string>

But I get a new error saying that A function whose declared type is neither 'void' nor 'any' must return a value.

Can someone please help me understand what I am doing wrong?

2
  • 2
    I think that when you do that : (async () => { , you're creating a new function, so this is that function that should have a return type Commented Oct 29, 2021 at 6:20
  • 1
    you are not returning anything, juste executing the arrow async function . just add return before (async()=>...). Commented Oct 29, 2021 at 11:50

3 Answers 3

6

You want to use await, so you need an async function. What you created is a self-invoking async function. But returning a value inside the self-invoking function does not return it for the base function.

What you are looking for is to make the base function async, and setting the return type to Promise<string>:

async exportPageAsText(pageNumber: number): Promise<string> {
  const text = await this.pdfViewerService.getPageAsText(pageNumber);
  console.log(text);
  return text;
}
Sign up to request clarification or add additional context in comments.

3 Comments

How do I subsequently convert a Promise<string> to string? Somewhere else, I use let text = this.pdfService.exportPageAsText(pageNumber); and then use .replace on text, but of course I get an error that .replace is a string method, not Promise<string> method. I cannot make that one async; it has to be converted to a normal string, so to speak...
In this case you have to directly work with promises, for example something like this.pdfService.exportPageAsText(pageNumber).then(text => { console.log(text); })
Or just const text: string = await this.pdfService.exportPageAsText(pageNumber).
1

Your method wait a return before the end but event the promise declared by async () => {... is not returned

so two thing to change in your code

  1. return the async()
  2. change the type of return in the methode declaration by Promise<string>

In your case syntax it will look like

exportPageAsText(pageNumber: number): Promise<string> {
    return (async () => {
        const text = await this.pdfViewerService.getPageAsText(pageNumber);
        console.log(text);
        return text;
    })();
}

Comments

1

This is how I would have done it. async/await.

    const exportPageAsText = async (pageNumber: number): Promise<string> => {
        const text: string = await this.pdfViewerService.getPageAsText(pageNumber);
        console.log(text);
        return text;
    }

Comments