I have having a recursive function and want to execute another function only when recursive function completed. My code is:
ngOnInit() {
this.func1(this.myArray.length).then(()=>{
this.func2()
}).catch()
}
func1(len: number) {
return new Promise ((resolve, reject) => {
setTimeout(() => {
console.log('in function func 1 --- ',this.myArray.length);
if (len > 0) {
this.myArray.pop();
this.func1(this.myArray.length).then().catch();
}
}, 1000);
resolve('func1 completed');
});
}
func2(){
console.log('in function func 2')
}
StackBlitz
Output in console:
in function func 2
in function func 1 --- 2
in function func 1 --- 1
in function func 1 --- 0
It should be like below:
in function func 1 --- 2
in function func 1 --- 1
in function func 1 --- 0
in function func 2
What is wrong here?
1