I've been trying to learn the JS concurrency model given a background in Python's.
Running the following in Python:
async def myFunction():
print("abc")
myFunction()
will print nothing. Running the following in JavaScript:
async function myFunction() {
console.log("abc")
}
myFunction()
will print "abc"
. In both cases I did not await
myFunction
, yet the evaluations were different.
I understand why the Python program prints nothing. myFunction
is a coroutine function that resolves to nothing, more specifically it returns an Awaitable[None]
. But to actually have this awaitable's side effect executed, I must await it.
I have also read Are promises lazily evaluated? with an answer of no, talking about how the eager evaluation of promises is guaranteed.
Even though I've looked at both concurrency models separately, their difference is still confusing. While general clarity about the contrast here would be very helpful, I also do have a specific question: Is there ever a point to awaiting a Promise
in JavaScript that resolves to nothing, and should only execute for its side effect? In other words, do await myFunction()
and myFunction()
possibly have a difference in behavior in a different case, even though they both gave the same output here?