react-native - 在 Redux 的操作中,使用回调调用函数(使用 async/await 和 React-Native-Contacts)
问题描述
我正在开发一个使用 React-Native、Redux 和 React-Native-Contacts 的移动应用程序。
在动作创建者中,我试图调用一个函数(来自 React-Native-Contacts),它具有回调(而不是承诺)。以下是我的代码。
我希望操作“acContactImport”等待辅助函数“fContactImport”完成,然后再继续执行“dispatch”语句。它不等待它。我怎样才能让它等待它?
// Action Creator:
import Contacts from 'react-native-contacts';
import { PermissionsAndroid } from 'react-native';
export const acContactImport = (userID) => {
return async (dispatch) => {
let contactList = [];
try {
// The following statement should be executed asynchronously.
// However, it is not. How can I make it asynchronous?
contactList = await fContactImport();
dispatch({
type: CONTACT_IMPORT,
payload: { contactList: contactList },
});
}
catch (err) {
console.log("acContactImport - catch: ", err);
}
};
};
// Helper Function 1
export const fContactImport = async () => {
let contactList = [];
if (Platform.OS === "android") {
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CONTACTS, {
title: "Contacts",
message: "This app would like to view your contacts."
})
.then(() => {
contactList = _loadContacts();
});
} else {
contactList = _loadContacts();
}
}
// Helper Function 2
export const _loadContacts = () => {
Contacts.getAll((err, data2) => {
if (err) {
return [];
}
else {
let candidates = [];
for (let i = 0; i < data2.length; i++) {
let candidateObject = { name: candidateRecord.givenName + " " + candidateRecord.middleName + " " + candidateRecord.familyName };
candidates.push(candidateObject);
}
return contactList;
}
});
}
解决方案
显然,问题不在于动作创建者,而在于辅助函数,它们不支持新的承诺风格(异步/等待)。所以,这里是解决方案...
// Action Creator:
// No changes are required. Original code is good.
// Helper Function 1
export const fContactImport = async () => {
let contactList = [];
if (Platform.OS === "android") {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
{
title: "Contacts",
message: "This app would like to view your contacts."
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
contactList = await _loadContacts();
} else {
console.log('Contacts access denied');
}
} catch (err) {
console.warn(err);
}
} else {
contactList = await _loadContacts();
}
return contactList;
}
// Helper Function 2
export const _loadContacts = () => {
return new Promise((resolve, reject) => {
Contacts.getAll((err, data2) => {
if (err) {
reject([]);
}
else {
let candidates = [];
for (let i = 0; i < data2.length; i++) {
let candidateObject = { name: candidateRecord.givenName + " " + candidateRecord.middleName + " " + candidateRecord.familyName };
candidates.push(candidateObject);
}
resolve(candidates);
}
});
});
}
推荐阅读
- cumulocity - Cumulocity 微服务在本地存储数据 - 大小上限?
- swift - 通过该值中的特定对象过滤掉对象
- python - 如何检查OLS中参数的p值
- regex - 在 Lua/PCRE/OpenResty 中使用正则表达式检测所有表情符号
- azure - 由于擦除,Blazor HttpClient GetAsync 超级慢
- cakephp - 如何在 cakephp 3.6 中同时向多个表中插入数据
- matlab - 在 ode MATLAB 求解器上使用中间值
- python - TensorFlow 2.0 dataset.__iter__() 仅在启用急切执行时受支持
- ios - iOS 启动屏幕忽略智能反转
- javascript - NodeJS:util.inspect对象,里面有函数