2

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.

Daniel B
  • 8,770
  • 5
  • 43
  • 76
Richard
  • 8,193
  • 28
  • 107
  • 228

1 Answers1

0

In your openDatabase():, you are not calling the resolve, try putting resolve(messagesData);

albert
  • 8,112
  • 3
  • 47
  • 63
Richard
  • 8,193
  • 28
  • 107
  • 228
  • Please either ask the user to make the comment into an answer or make your answer community wiki. – Jed Fox Oct 05 '16 at 16:47
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/13889845) – Sherif Oct 05 '16 at 18:42
  • @Sherif Actually, it does. – Bergi Oct 05 '16 at 23:07