2016-11-08 135 views
1

我有两个函数,我想调用,但我只想在第一个函数完成后调用第二个函数。我该如何去做呢?TypeScript/Angular 2 - 在另一个完成后调用一个函数

首先功能:

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); 
    }); 
} 

回答

3

有两种简单的方法,第一个完成之后打电话给你的第二个功能 - 你可以this.nodeChildren = res;下做到这一点或使用完成参数()

getDirectorySubfolders(node: any) { 
    console.log('Home:getDirectorySubfolders() entered...'); 

    this._sdiService.getDirectoriesAtPath("Desktop") 
     .subscribe(res => { 
      this.nodeChildren = res; 
      this.getChildren(); <-- here 
     }, 
     () => { 
      this.getChildren(); <-- or here 
     }); 
} 

当您拨打getDirectorySubfolders()函数时,getChildren()将在完成getDirectorySubfolders()后调用。

相关问题