Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
73
rated 0 times [  76] [ 3]  / answers: 1 / hits: 90906  / 7 Years ago, sat, may 27, 2017, 12:00:00
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>?


More From » typescript

 Answers
6

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

[#57641] Wednesday, May 24, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lindsay

Total Points: 402
Total Questions: 109
Total Answers: 109

Location: Tuvalu
Member since Sat, Feb 11, 2023
1 Year ago
;