【发布时间】:2018-04-22 20:46:52
【问题描述】:
所以,如果我的 Ionic/Angular/TypeScript 项目中有这样的代码...
let arr: Array<string> = [];
this.databaseProvider.getAllSpecies().then(allSpecies => {
for(let species of allSpecies) {
if(species.name.toLowerCase().indexOf(keyword.toLowerCase()) > -1
|| species.latinName.toLowerCase().indexOf(keyword.toLowerCase()) > -1) {
arr.push(species.name + " - " + species.latinName);
}
}
});
return arr;
...arr,当然返回的时候会为空,因为then()这个handler还没有被执行。
有没有办法从 Promise 处理程序返回字符串数组?目前,我已将所有 species 对象从数据库加载到内存中,但我不想那样做,因为会有数百个。
代码是来自ionic2-autocompletegetResults()函数的sn-p,它必须返回一个字符串数组,即不是另一个Promise。
【问题讨论】:
-
ionic2-autocomplete 文档声明
getResults被允许返回一个承诺(或可观察的,或常规值)。 -
我刚刚注意到同样的事情,但由于某种原因,这似乎不适用于我的应用程序。
标签: angular typescript ionic-framework promise