Ask HN: Why can't await be used from non-async functions?
Summary: Explicit &#x27;async&#x27; functions avoid implicit messiness and allow control over which sections of code are running at one time.<p>&gt; From what I understand, in JavaScript at least, putting `await foo()` inside an async function, splits the calling function in two, with the 2nd half being converted to a callback<p>I wouldn&#x27;t say that&#x27;s wrong, but it may be a slight over-simplification if your mental-model isn&#x27;t complete.
For example you can await from within a loop which would make function-splitting kind of a strange thing to think about.
But yes, every `await` is a suspension point that will be resumed and the function carries on, so effectively
the rest of the function becomes a callback, but let&#x27;s call it a &quot;continuation&quot;, which is the callback that
continues from where we left off.<p>Of course it&#x27;s implementation specific, but at a high level these continuations are basically normal functions which
may return either the &quot;incomplete&quot; pair (new_awaitable, next_continuation) for `await new_awaitable` statements, or the
&quot;complete&quot; return-value for `return` statements. (Maybe other things like exceptions, but let&#x27;s not go there)<p>It&#x27;s the event-loop&#x2F;task-scheduler which keeps track of these callback&#x2F;continuations: when one returns with a complete-value,
the scheduler will go find the continuation that was awaiting it, and call that with the new value. If it returns a new
(awaitable, continuation) object, the loop will start the awaitable and repeat until it returns a value, then resume the continuation with the value.<p>So you can think of await as &quot;return to the event loop with awaitable and keep calling the callbacks until a final value is returned&quot;.<p>This is where the function coloring comes from: async functions need access to that event loop to keep returning
to and continuing from, they know they&#x27;re being called in a special way and return these special values for await&#x2F;return that
the event loop knows how to deal with; normal functions don&#x27;t have access and just return values in the &quot;normal&quot; way.<p>Of course, to reap any benefit from this you have to have multiple tasks running &quot;at the same time&quot;. To just await one thing which awaits one
thing is not much different than a normal function call&#x2F;stack. But when you&#x27;re listening on sockets and fetching HTTP resources
and making database queries and waiting for user responses, each executing simultaneously but on one thread (so no thread contention),
that&#x27;s when async shines. But note: they all must be talking to the same event loop which manages all these coroutines, running
one at a time.<p>But what happens if &quot;normal&quot; functions could await?<p>Put another way: effectively de-color functions by making them all &quot;async&quot; and thus able to await. (Unless I&#x27;m misinterpreting the initial question)<p>From what I said above, all we have to do is change the implementation such that when a function returns, internally it actually this
special &quot;complete&quot; variable type (with the real returned value inside). Then, with an event loop you get normal behavior and forward
to whatever function had called it. Then you just add the awaits which return &quot;incomplete&quot; variables and you get waiting behavior.<p>This would work, BUT it introduces an overhead of going to the event loop checking if the return is &quot;complete&quot;, finding the caller,
and forwarding it for EVERY function call. Rather than the usual stack popping&#x2F;register storing of standard languages.
Optimizable? yeah probably, but it&#x27;s not zero-cost, especially true in languages like Python which doesn&#x27;t have an implicit event loop like JavaScript,
so you&#x27;re making huge changes to the implementation to begin with.<p>We could approach from the other side: returns stay the same, but the act of calling await creates an event loop or some object
which does the &quot;callback-the-callbacks&quot; routine.
I guess this would be the stop-the-world approach? This one function would keep looping through the callbacks until a final value reached.
On second thought, this isn&#x27;t different than a standard function call, because any internal `awaits` would setup their own loops and you always
end up with a single returned value (or infinite loop). There&#x27;s no way for multiple loops to running the continuations.<p>If you really don&#x27;t like the keyword &quot;async&quot; before your function definition you could implement
implicit async functions: just make any function in your language which includes an `await` implicitly become &quot;async&quot;.
But this only kicks the can down the road, as if you use await, that means you&#x27;re returning continuations, and any
callers need to await your value, and therefore your function is now implicitly colored.<p>And how do you implement the equivalent of<p><pre><code>    async function() { return 42; }
</code></pre>
maybe?<p><pre><code>    function() { return 42; await; }

</code></pre>
Implicit event loop.<p>Maybe this is the core of the question: for a language like JavaScript which already has an event loop, why can&#x27;t we do<p><pre><code>    function foo() { let x = await whatever(); return x; }
    console.log(foo());
</code></pre>
The await in a non-async function could be implemented such that it goes out to the global event loop, &quot;registers&quot; itself as dependent on
the Promise returned from &quot;whatever()&quot; then sit and wait for the event loop to return the value. This should sound familiar to you as
the behavior of the standard async function &#x2F; continuation business, but now we&#x27;re trying to say that foo doesn&#x27;t return a stream of continuations,
(it just takes a long time to run).<p>While this may be doable, a problem arises if foo was called from within an async function. Part of &quot;contract&quot; of async programming is your function is
run as usual between calls to await; at those points you return to the event loop and let other continuations run, but until the next await,
shared&#x2F;global variables will remain untouched because nobody else is running. If you called foo() which then starts running `whatever` on the event loop
again any guarantees about ordering of events or state of variables may change unexpectedly.