javascript - Order promises execution in AngularJs -


i'm working on angular full-stack es6 , babel.

in controller, have:

$oninit() {      this.$http.get('/api/example')         .then(() => {console.log("task1")})         .then(() => {console.log("task2")}) } 

the console result want:

 task1    task2 

but when try refactor code:

$oninit() {      this.$http.get('/api/example')         .then(() => {console.log("task1")})         .then(afunction()) }    afunction() {     console.log("task2") } 

the console result is:

 task2    task1 

why happens ?

nb: .then(() => {this.afunction()}); seems work not seems clean solution.

you should passing function reference .then(afunction) instead of function call. doing afunction() invoking function.

$oninit() {      this.$http.get('/api/example')         .then(() => {console.log("task1")})         .then(afunction) } 

Comments