I have two functions that I would like to call, but I only want to call the second one after the first one completes. How do I go about doing that?
First function:
getDirectorySubfolders(node: any) {
console.log('Home:getDirectorySubfolders() entered...');
this._sdiService.getDirectoriesAtPath("Desktop")
.subscribe(res => {
this.nodeChildren = res;
});
}
Second function:
getChildren(node: any) {
console.log('Home:getChildren entered..');
return new Promise((resolve, reject) =>
{
setTimeout(() => resolve(this.nodeChildren.map((c) =>
{
return Object.assign({}, c, {});
})), 1000);
});
}
There are two simple ways to call your second function after first one is finished - you can do it under this.nodeChildren = res;
or use finish parameter ()
:
getDirectorySubfolders(node: any) {
console.log('Home:getDirectorySubfolders() entered...');
this._sdiService.getDirectoriesAtPath("Desktop")
.subscribe(
res => {
this.nodeChildren = res;
this.getChildren(); <-- here
},
err => {
console.log(err);
},
() => {
this.getChildren(); <-- or here
});
}
When you call getDirectorySubfolders()
function, getChildren()
will be called after completion of getDirectorySubfolders()
. Keep in mind that if you use finish parameter, the function will get called even if error occurs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With