Does a async function which returns Promise<void> have an implicit return at the end of a block?

JavascriptTypescriptEs6 Promise

Javascript Problem Overview


public async demo(): Promise<void> {
    // Do some stuff here
    // Doing more stuff
    // ... 
    // End of block without return;
}

Is a new Promise<void> returned implicitely at the end of the block in TypeScript/ES6?

Example for boolean type:

class Test {

    public async test(): Promise<boolean> {
        return true;
    }

    public main(): void {

        this.test().then((data: boolean) => {

            console.log(data);

        });

    }

}

new Test().main();

This prints true to the console because a return inside of a async function creates a new Promise and calls resolve() on it with the returned data. What happens with a Promise<void>?

Javascript Solutions


Solution 1 - Javascript

> What happens with a Promise

Same thing with a function that returns void. A void function returns undefined. A Promise<void> resolves to an undefined.

function foo(){}; console.log(foo()); // undefined
async function bar(){}; bar().then(res => console.log(res)); // undefined

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGalaView Question on Stackoverflow
Solution 1 - JavascriptbasaratView Answer on Stackoverflow