此代码在服务器响应之前返回。我怎么让它等待?
public loadDropBoxItemsFromServer(preventSaveToCache: boolean = false): Promise<IDropboxItems> {
let hcpid = this.authService.getUserInfo().userId;
this.log("Loading d-box from server");
CredDropboxProvider._loadDropboxFromServerPromises = new Promise((resolve, reject) => {
this.http.get(this.appSettingsService.getApiUrl(`/CredDoc/list/${hcpid}`), this.appSettingsService.getHttpRequestOptions(this.authService))
.map((response: Response) => {
this.log("Got a response");
this.log(response.json());
return response.json();
})
.timeoutWith(this.appSettingsService.defaultHttpTimeout, Observable.throw(new HttpTimeoutError(this.appSettingsService.timeoutErrorText)))
.subscribe((dropbox: IDropboxItems) => {
this.log("Dropbox loaded from server");
if (!dropbox.items) {
dropbox.items = []
}
if (preventSaveToCache) {
CredDropboxProvider._loadDropboxFromServerPromises = null;
resolve(dropbox);
} else {
CredDropboxProvider._loadDropboxFromServerPromises = this.saveDropboxToCache(dropbox)
.then((newDropbox: IDropboxItems) => {
this.log("New Dropbox");
console.log(newDropbox);
CredDropboxProvider._loadDropboxFromServerPromises = newDropbox;
resolve(newDropbox);
})
.catch(error => {
CredDropboxProvider._loadDropboxFromServerPromises = null;
reject(error);
});
}
},
this.appSettingsService.buildStandardHttpErrorHandler(reject)
);
});
console.log("Returning ");
**THIS IS RETURNING NULL BECAUSE IT RETURNS BEFORE THE DATA IS RETURNED **
return CredDropboxProvider._loadDropboxFromServerPromises[hcpid];
}
以上是在我的提供商和我的.ts文件中我称之为:
this.CredDropBox.loadDropBoxItemsFromServer().then((db: IDropboxItems) => {
console.log("Loading DB Items");
console.log(db);
this.dropbox = db; ** THIS IS ALWAYS UNDEFINED **
});