Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript“承诺”如何使同步_Javascript_Sqlite_Typescript - Fatal编程技术网

Javascript“承诺”如何使同步

Javascript“承诺”如何使同步,javascript,sqlite,typescript,Javascript,Sqlite,Typescript,我正在使用Ionic2/Typescript 我有两个承诺,我想在我继续之前完成,即同步。所以我把对这两个函数的调用放在一个承诺中。全部…,期望它们在调用resolve之前完成 我有以下代码: public openDatabase(): Promise<Array<Message>> { let promise: Promise<Array<Message>> = new Promise<Array<Message>&g

我正在使用Ionic2/Typescript

我有两个承诺,我想在我继续之前完成,即同步。所以我把对这两个函数的调用放在一个承诺中。全部…,期望它们在调用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);
        });
}

对于如何构建我的承诺的任何建议,我都非常感谢。

您需要从内部承诺回访中返回新的承诺:

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);
        });
}
注意,返回Promise.all并返回this.database.openDatabase

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);
        });
}