then()
当顺序重要时必须嵌套。所以呢
dosomething1.then( ()=> {
dosomething2.then( () => {
dosomething3.then( () => {
console.log("hey");
})
})
})
对你来说那意味着
refreshDashboard() {
console.log('refreshing...');
this.getDashboardData();
// Get labels
this.database
.createDatabase()
.then(() => {
this.getLabels();
})
.then(() => { <---This here has no use at all. Nothing returns a promise here
});
}
// Coming from an API call, which returns an Observable
getDashboardData() {
this.dashboardService.getData().subscribe((data) => {
this.rows = data;
});
}
// Coming from local SQLite database
getLabels() {
this.database
.getLabels()
.then((data) => {
this.labels = [];
if (data.rows.length > 0) {
for (let i = 0; i < data.rows.length; i++) {
this.labels.push(data.rows.item(i));
}
}
console.log(this.rows.length); // here you expect values to be ready
console.log(this.labels.length); // here you expect values to be ready
})
.catch((e) => {
alert('There was an error: ' + e);
});