Skip to content

Instantly share code, notes, and snippets.

@gskachkov
Created June 19, 2017 07:38
Show Gist options
  • Save gskachkov/19ef2f1082b9654a6ec20e098a97a626 to your computer and use it in GitHub Desktop.
Save gskachkov/19ef2f1082b9654a6ec20e098a97a626 to your computer and use it in GitHub Desktop.
Using async function in angular 1.5
const _fooService = new WeakMap();
class FooController {
constructor(fooService) {
this.name = 'fooController';
_fooService.set(this, fooService);
this.title = 'Show data';
this.asyncResult = 'No result';
this.doFoo();
}
async loadData() {
this.status = 'loading';
return _fooService.get(this)
.getData()
.catch();
}
async doFoo() {
this.asyncResult = await this.loadData();
this.status = 'loaded';
}
}
FooController.$inject = [ 'mypageService' ];
export default FooController;
const data = [ {id: 10, value:'value-1'}, {id: 10, value:'value-1'}];
class FooService {
async getData() {
const ph = {};
const promise = new Promise(resolve => {
ph.resolve = resolve;
});
// Make it async
setTimeout(() => ph.resolve(data), 100);
return promise;
}
static factory() {
return new FooService();
}
}
FooService.factory.$inject = [ ];
export default FooService.factory;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment