async/await improvements

Tab Atkins Jr. jackalmage at
Wed Nov 12 16:24:31 PST 2014

On Wed, Nov 12, 2014 at 4:18 PM, James Long <longster at> wrote:
> On Wed, Nov 12, 2014 at 7:14 PM, Tab Atkins Jr. <jackalmage at> wrote:
>> On Wed, Nov 12, 2014 at 4:07 PM, James Long <longster at> wrote:
>>> Maybe this would be resolved if you could answer this: how do you mark
>>> an async function to be a top-level one? I don't see anywhere that
>>> says "I don't return a promise, I want errors inside of me to
>>> literally throw. I am an all-powerful top-level consumer of async
>>> stuff"). Seems like that would need extra syntax?
>> There is currently no way to do so.  If there was, they still wouldn't
>> "literally throw", because again, that's impossible - by the time the
>> promise rejects, it may be another turn entirely, and the program
>> counter is long past the callsite.  The best it can do is be an
>> automatically-unhandled error, caught by window.onerror.
> You do realize that generators have a `throw` method, and way long
> after the generator is yielded you can throw and error from the yield
> point and current devtools will correctly pause at that point if you
> enable "pause on uncaught exceptions"? A top-level async function
> would throw in exactly this same way when an a promise that `await` it
> waiting for fails.

Yes, that's exactly what I mean by "automatically-unhandled error".
It's impossible to catch an asynchronous error from the synchronous
code that called it, but you can appeal to a global environment to
catch it.

> This really seems like a huge oversight that there isn't a way to mark
> an async function as top-level. When async/await gets here people are
> going to want to use that *everywhere*, as they should, and forcing
> them to only interact with them as middle-men just so that you can
> call `.done()` on an old-style promise chain is weird.

Marking a function as "top-level" is just syntax sugar for calling
.done() on the returned promise.  (Assuming that .done() can forward
rejections to window.onerror.)  It might be useful, I dunno, but it
doesn't offer anything fundamentally new.


More information about the es-discuss mailing list