Created
June 19, 2017 07:38
-
-
Save gskachkov/19ef2f1082b9654a6ec20e098a97a626 to your computer and use it in GitHub Desktop.
Using async function in angular 1.5
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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