async/await is not equivalent to using promises. It provides fundamentally different functionality by wrapping promises with a generator. It is important to recognize the difference, because it affects error handling in a significant way.
In this example, if somethingElse() or anotherThing() throw an exception, we would want doSomething() included in the stack trace, since that's where somethingElse() and anotherThing() are called from. In order to do that, the Javascript engine has to capture and store a trace of the stack where doSomething() is called before moving into somethingElse() since it will be inaccessible after somethingElse() is on the stack. That operation consumes both memory and time.
Using await, there is no need to copy and store the current stack context before the execution of somethingElse(). Simply storing a pointer from somethingElse() to doSomething() is sufficient: during the execution of somethingElse(), doSomething() is still on the stack with its execution suspended, so it's available from inside somethingElse(). If somethingElse() throws an exception, the stack-trace can be reconstructed by traversing the pointer. If anotherThing() throws an exception, the stack-trace can be reconstructed as normal, because we are still within the context of doSomething(). Either way, capturing and storing a stack trace in memory before doSomething completes is no longer necessary, and constructing a stack-trace only happens when its necessary, i.e. an actual exception was thrown from somethingElse() or anotherThing().
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
async
/await
is not equivalent to using promises. It provides fundamentally different functionality by wrapping promises with a generator. It is important to recognize the difference, because it affects error handling in a significant way.Consider:
In this example, if
somethingElse()
oranotherThing()
throw an exception, we would wantdoSomething()
included in the stack trace, since that's wheresomethingElse()
andanotherThing()
are called from. In order to do that, the Javascript engine has to capture and store a trace of the stack wheredoSomething()
is called before moving intosomethingElse()
since it will be inaccessible aftersomethingElse()
is on the stack. That operation consumes both memory and time.Now consider:
Using
await
, there is no need to copy and store the current stack context before the execution ofsomethingElse()
. Simply storing a pointer fromsomethingElse()
todoSomething()
is sufficient: during the execution ofsomethingElse()
,doSomething()
is still on the stack with its execution suspended, so it's available from insidesomethingElse()
. IfsomethingElse()
throws an exception, the stack-trace can be reconstructed by traversing the pointer. IfanotherThing()
throws an exception, the stack-trace can be reconstructed as normal, because we are still within the context ofdoSomething()
. Either way, capturing and storing a stack trace in memory beforedoSomething
completes is no longer necessary, and constructing a stack-trace only happens when its necessary, i.e. an actual exception was thrown fromsomethingElse()
oranotherThing()
.