Slbox
Slbox

Reputation: 13138

How does Chrome's garbage collector handle async functions that never return a value?

Does this cause any issues? I'd guess not, as long as we don't attempt to await an async function without a return value?

I've been looking for information on this, and I'm sure it's out there (maybe even obvious) but I haven't had any luck finding a definitive answer.

Upvotes: 0

Views: 109

Answers (1)

jmrk
jmrk

Reputation: 40681

The garbage collector has nothing to do with it.

In JavaScript, every function returns a value. When it has no explicit return statement, it'll implicitly return undefined.

Of course, if you wait for an event that never happens, you'll end up waiting forever. Any callbacks registered to run when the event happens will wait with you, i.e. they'll stay in memory forever.

See for yourself:

async function foo() { /* no explicit return */ }
let result = await foo();
console.log(result);  // undefined

Upvotes: 1

Related Questions