Tab Atkins Jr.
jackalmage at gmail.com
Wed Nov 12 15:18:03 PST 2014
On Wed, Nov 12, 2014 at 3:06 PM, James Long <longster at gmail.com> wrote:
> On Wed, Nov 12, 2014 at 5:56 PM, Tab Atkins Jr. <jackalmage at gmail.com> wrote:
>> It's impossible to rethrow errors without use of "await" - the error
>> may happen in a different turn entirely than the function call. You
>> *must* convert your calling function into an async one, so that it can
>> do the "freeze and wait for the promise to resolve" thing, which means
>> that it needs to return a promise for itself as well.
> The crucial different from what you're describing (not directly in the
> paragraph above) is that you say propagate by default, and I say throw
> by default (and a real throw, like uncaught exception in the debugger
> type throw). The latter imposes way less constraints on the GC and
> less ambiguity of error handling, at the cost of slightly more verbose
> async handling. I was hoping async/await may be a chance to look at
> this, but it sounds like we're far down the road of promises for it to
> be an option.
No, you're misunderstanding me, or the way that async stuff works.
Calling an async function returns immediately. The called function
doesn't actually run until a later turn. If it throws, there's *no
way*, even theoretically, to throw that error at the call-site,
because the program counter is already well past that point.
If you want the call-site to throw, then you need the callsite itself
to be in an async function, and you need to use the "await"
expression, which pauses execution of the caller until the callee's
returned promise settles. At that point you can throw the rejection
value, or return the fulfillment value.
This isn't something you can make an arbitrary decision on; it's not a
syntax matter. It fundamentally changes the way your code works, and
you cant' get around it.
The only thing we can actually talk about changing one way or the
other is whether unhandled rejections immediately go to the console,
or if they only do so upon GC/when you call .done(). That really is
something that we can vary just by messing around with syntax.
More information about the es-discuss