Using async/await function in Angular 1.5 + Babel 6

olexandr
1 min readJun 19, 2017

--

As async/await function is became a part of ES2017 spec, our team decided to start use it in project. It is great that async/await function also supported by Babel, so we could play with in current project that target some old browsers. So we make prototype of the solution with angular service and controller that use async function:

It works fine except one problem that async function is executed out of angular digest it returned data appears only after some force scope update, by scope.apply, but code start looks not very simple. After some research it was decided to use implementation of decorators. Babel contains transpiler for decorators. It has difference from current version of the spec, but it allow to solve our problem. So we create simple decorator and that add rooScope.$applyAsync to promise chain into target async method of the controller.

So after this modification decorator controller, still looks ok and we do need add scope.$apply in each method

--

--

olexandr

Webkit JavaScriptCore contributor, Frontend TechLead