Have a look at the following Angular 1.x controller:
class RootController
{
constructor($http)
{
this.variable = "apples";
// this.test($http); -- this works
// this.asyncTest($http); -- this doesn't
}
async asyncTest($http)
{
await $http.get('/api/someApiCall');
this.variable = "oranges";
// await $http.get('/api/someApiCall'); -- this makes this method work
}
test($http)
{
$http.get('/api/someApiCall').then(() => {
this.variable = "oranges";
});
}
}
If we uncomment one of the two commented out lines in the constructor, we get different behavior. If we run test(), then variable gets updates to the value "oranges" and displays in the view, as expected. If we run asyncTest(), the view continues to display "apples" until something forces a digest (for some reason clicking in and out of a textbox seems to do the trick).
Another weird issue is that if we uncomment out the second await in asyncTest(), then that method now works as expected.
Why does this happen, and why does the second await fix it? I thought await was pretty much just syntactic sugar for then, so these should be completely equivalent. I found some articles talking about this issue but they're more about how to fix it than what's going on under the hood.