【发布时间】:2019-07-02 08:36:33
【问题描述】:
我正在尝试从我的 AsyncStorage 数据库中获取所有密钥,然后在另一个函数中过滤它们,似乎无法让它等到 AsyncStorage 返回数据?
此函数返回数组中的键:
DATABASE_getAllCoffeeEntries = () => {
AsyncStorage.getAllKeys((err, keys) => {})
.then(keys => {
AsyncStorage.multiGet(keys, (error, items) => {
return items;
}).then(items => {
return items; // array of items is returned
});
});
}
这个函数是用来调用上面的函数然后等待结果然后过滤数据。
somefunc = async () => {
var items = await DATABASE_getAllCoffeeEntries();
var someItems = items.filter(function (result, i, item) {
// do filtering stuff
return item;
});
// do something with filtered items
}
在这里尝试了很多,但我无法理解...任何帮助都会很棒,谢谢。
【问题讨论】:
-
getAllCoffeeEntries 应该是使用 await 关键字调用的异步函数,
标签: react-native asynchronous async-await asyncstorage