Reputation: 3636
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);
});
}
Upvotes: 5
Views: 15131
Reputation: 50643
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.
Upvotes: 9