我不知道如何遍歷數組併發送每個項目鏈接的承諾。我試圖在下面定義DBSchema.executeMigrations
(詳情請參閱評論)。如何鏈接承諾從靜態數組參數在打字稿
假設migrations
會隨着時間而增長。所以,現在只有3個參數,但明年可能會達到100個。我需要這個迭代。
export class DBConnection {
exec(query): Promise<any> {
let _this = this;
return new Promise((resolve) => {
_this.get().then((db) => {
db.executeSql(query, {}).then(
(result) => {
resolve(result);
},
(err) => {
console.log('Unable to execute query: ' + err.message);
}
);
});
});
}
}
export class DBSchema {
constructor(public db_connection: DBConnection){};
static migrations = [
"CREATE TABLE IF NOT EXISTS events(id INTEGER PRIMARY KEY NOT NULL, title STRING)",
"CREATE TABLE IF NOT EXISTS news(id INTEGER PRIMARY KEY NOT NULL, title STRING)",
"CREATE TABLE IF NOT EXISTS whatever(id INTEGER PRIMARY KEY NOT NULL, title STRING)"
];
executeMigrations(): any {
// send each migration to db_connection.exec
// do it in order
// return a promise that resolves when all of them are done
}
}
乾杯,夥計,感謝您的快速和真棒迴應...... exec()中'then'行的什麼部分是不必要的? – jsharpe
不客氣!如果你仔細想一下,'res'的值是從'db.executeSql()'返回/解析的,所以不需要添加額外的'.then()'來重新返回結果。我將它包含在代碼中僅僅是爲了說明實際返回的結果。看看[這個jsfiddle](https://jsfiddle.net/0sL1ffa4/)來演示它! –