我想调用两个函数,但我只想在第一个函数完成后调用第二个函数。我该怎么做呢?
第一个函数:
getDirectorySubfolders(node: any) {
console.log('Home:getDirectorySubfolders() entered...');
this._sdiService.getDirectoriesAtPath("Desktop")
.subscribe(res => {
this.nodeChildren = res;
});
}第二个函数:
getChildren(node: any) {
console.log('Home:getChildren entered..');
return new Promise((resolve, reject) =>
{
setTimeout(() => resolve(this.nodeChildren.map((c) =>
{
return Object.assign({}, c, {});
})), 1000);
});
}发布于 2016-11-08 12:40:29
在第一个函数完成后,有两种简单的方法来调用您的第二个函数??您可以在this.nodeChildren = res;下这样做,或者使用finish参数()。
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
});
}当您调用getDirectorySubfolders()函数时,getChildren()将在getDirectorySubfolders()完成后被调用。请记住,如果使用finish参数,即使发生错误,函数也会被调用。
https://stackoverflow.com/questions/40486765
复制相似问题