Waiting for a function that is not even called is a futile endeavor. Surprisingly, neither a compile time error nor a runtime error is generated in such cases. The perplexing question remains - in what scenarios would one wait for a function to be executed?
In the given code snippet, foo
remains dormant and goes unused. One might wonder why TypeScript does not flag this as an error.
async function foo(){
console.log("foo");
}
async function bar(){
//correct usage should be: await foo();
await foo;
}
bar();
//the resulting es6 code.
function foo() {
return __awaiter(this, void 0, void 0, function* () {
});
}
function bar() {
return __awaiter(this, void 0, void 0, function* () {
yield foo;
});
}
bar();