我有以下的Typescript代码。我有promise
我需要完成并返回。但是,promise
未按预期工作。任何建议表示赞赏。
chats.ts
private loadChats(): void {
let promiseRefreshedLocalDatabase: Promise<any> = this.chatsStorageService.openDatabase();
promiseRefreshedLocalDatabase.then((data) => {
console.log('--> in chat promise: data '+data);
});
}
chatsStorageService.ts
public openDatabase(): Promise<any> {
let promise: Promise<any> = new Promise<any>(resolve => {
this.database = new SQLite();
this.database.openDatabase({ name: "data.db", location: "default" }).then(() => {
let promiseChats: Promise<Array<Chat>> = this.refreshChats();
return promiseChats.then((chatsData) => {
let promiseMessages: Promise<Array<Message>> = this.refreshMessages();
return promiseMessages.then((messagesData) => {
});
});
}, (error) => {
console.log("OPEN ERROR: ", error);
});
});
return promise;
}
输出
我希望:
refreshChats refreshMessages --> in chat promise: data [object]
但只能来自this.refreshChats()
和this.refreshChats()
的电话:
refreshChats refreshMessages
即。 console.log('--> in chat promise: data '+data);
未执行。
答案 0 :(得分:0)
在你的openDatabase():中,你没有调用解决方案,试着放resolve(messagesData);