Javascript'承诺'如何制作同步

时间:2016-10-19 06:36:31

标签: javascript sqlite typescript

我正在使用Ionic2 / Typescript。

在我继续(即同步)之前,我有2 Promise个我想完成的事情。所以我调用了Promise.all(...)中的2个函数,希望它们在调用resolve之前完成。

我有以下代码:

public openDatabase(): Promise<Array<Message>> {
    let promise: Promise<Array<Message>> = new Promise<Array<Message>>(resolve => {
        if (false && this.database && this.database != null) {
            Promise.all([this.refreshChats(this.database), this.refreshMessages(this.database)]).then(() => {
                console.log('openDatabase1: resolve');
                resolve(this.messages);
            });
        } else {
            this.database = new SQLite();
            this.database.openDatabase({
                name: "data.db",
                location: "default"
            }).then(() => {
                Promise.all([this.refreshChats(this.database), this.refreshMessages(this.database)]).then(() => {
                    console.log('openDatabase2: resolve');
                    resolve(this.messages);
                });
            }, (error) => {
                console.log("OPEN ERROR: ", error);
            });
        }
    });
    return promise;
}

public refreshChats(db: any): Promise<any> {
    console.log('refreshChats ');
    return db.executeSql("SELECT * FROM chats", [])
        .then((chatData) => {
            let promises: Array<any> = [];
            this.chats = [];
            if (chatData.rows.length > 0) {
                for (var i = 0; i < chatData.rows.length; i++) {
                    promises.push(this.populateChat(db, chatData.rows.item(i)));
                }
            }
            Promise.all(promises).then(() => {
                console.log('refreshChats return this.chats.length = ' + this.chats.length);
                return this.chats;
            });
        })
        .catch(error => {
            console.log("ERROR REFRESHING CHATS: " + JSON.stringify(error));
            console.log(error);
        });
}

从我的控制台输出中,您可以看到在聊天完成更新之前调用了resolve

refreshChats
populateChat  Object {...}
openDatabase2: resolve
refreshChats return this.chats.length = 1

对于如何构建我的Promises的任何建议表示赞赏。

1 个答案:

答案 0 :(得分:1)

你需要从内部承诺回调中返回新的承诺:

public openDatabase(): Promise<Array<Message>> {
    let promise: Promise<Array<Message>> = new Promise<Array<Message>>(resolve => {
        if (false && this.database && this.database != null) {
            return Promise.all([this.refreshChats(this.database), this.refreshMessages(this.database)]).then(() => {
                console.log('openDatabase1: resolve');
                resolve(this.messages);
            });
        } else {
            this.database = new SQLite();
            return this.database.openDatabase({
                name: "data.db",
                location: "default"
            }).then(() => {
                return Promise.all([this.refreshChats(this.database), this.refreshMessages(this.database)]).then(() => {
                    console.log('openDatabase2: resolve');
                    resolve(this.messages);
                });
            }, (error) => {
                console.log("OPEN ERROR: ", error);
            });
        }
    });
    return promise;
}

public refreshChats(db: any): Promise<any> {
    console.log('refreshChats ');
    return db.executeSql("SELECT * FROM chats", [])
        .then((chatData) => {
            let promises: Array<any> = [];
            this.chats = [];
            if (chatData.rows.length > 0) {
                for (var i = 0; i < chatData.rows.length; i++) {
                    promises.push(this.populateChat(db, chatData.rows.item(i)));
                }
            }
            return Promise.all(promises).then(() => {
                console.log('refreshChats return this.chats.length = ' + this.chats.length);
                return this.chats;
            });
        })
        .catch(error => {
            console.log("ERROR REFRESHING CHATS: " + JSON.stringify(error));
            console.log(error);
        });
}

注意,return Promise.allreturn this.database.openDatabase