在我的情況下,我必須獲取customerService上的客戶列表並返回到組件。請任何人都可以通過重寫getCustomersList方法來幫助我。Angular 2承諾不等待解決嵌套承諾?
import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';
@Injectable()
export class CustomerService {
private sDBName:string;
private db;
private isDBExist:boolean = false;
constructor() {}
setDBName(sDBName:string) {
this.sDBName = sDBName;
}
connect():Promise<any> {
this.db = new SQLite();
return this.db.openDatabase({
name: this.sDBName,
location: 'default'
});
}
getCustomersList():Promise<any> {
return Promise.resolve(()=>{
return this.connect().then(()=>{
this.isDBExist = true;
let sql = 'SELECT * FROM customer ORDER BY customer_id DESC LIMIT 10';
return this.db.executeSql(sql, {}).then((result)=>{
let customers = [];
for(let i=0; i<result.rows.length; i++) {
customers.push(result.rows.item(i));
}
return customers;
},(err)=>{
this.debug('Unable to select customers', err);
return [];
});
},(err)=>{
this.debug('Unable to open database', err);
return [];
});
});
}
}
這個回報仍然不會工作,因爲在第一個連接的承諾之內還有一個承諾存在,所以這將不會返回任何我猜。 return語句不會等待內部承諾完成 – Sundar
@Sundar只是更多地瞭解承諾。當'.then'函數返回另一個承諾時,它會等到它解析後再運行下一個'.then' – smnbbrv