I have the following Typescript code. I have a promise
that I need to complete and return. However, the promise
is not working as expected. Any advise is appreciated.
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;
}
output
I would expect:
refreshChats refreshMessages --> in chat promise: data [object]
but only get from the calls to this.refreshChats()
and this.refreshChats()
:
refreshChats refreshMessages
i.e. console.log('--> in chat promise: data '+data);
is not executed.