Я пытаюсь выяснить правильную реализацию запуска Asyn c Action Creator внутри Asyn c Redux Thunk Action Creator. Какой из следующих методов является правильным?
Метод 1:
export const loginUser = () => async(dispatch) => {
try {
user = await LoginManager.logInWithPermissions(['public_profile', 'email']);
dispatch({type: LOGIN, payload: user});
dispatch({type:SAVE_USER_TO_DEVICE, payload:saveUserToDevice(user)});
} catch (error) {
console.log(error);
}
}
export const saveUserToDevice = async(user)=> {
try {
//saveUserToDeviceStatus returns true if successful, otherwise returns an object
const saveUserToDeviceStatus = await Keychain.setGenericPassword('user', JSON.stringify(user));
return (saveUserToDeviceStatus? true: false);
} catch (error) {
console.log(error);
}
}
Метод 2: saveUserToDevice отправка
export const loginUser = () => async(dispatch) => {
try {
user = await LoginManager.logInWithPermissions(['public_profile', 'email']);
dispatch({type: LOGIN, payload: user});
saveUserToDevice(user);
} catch (error) {
console.log(error);
}
}
export const saveUserToDevice = (user) => async(dispatch) => {
try {
//saveUserToDeviceStatus returns true if successful, otherwise returns an object
const saveUserToDeviceStatus = await Keychain.setGenericPassword('user', JSON.stringify(user));
saveUserToDeviceStatus = saveUserToDeviceStatus? true: false;
dispatch({type: SAVE_USER_TO_DEVICE, payload: saveUserToDeviceStatus});
} catch (error) {
console.log(error);
}
}
Метод 3: saveUserToDevice возвращает объект действия
export const loginUser = () => async(dispatch) => {
try {
user = await LoginManager.logInWithPermissions(['public_profile', 'email']);
dispatch({type: LOGIN, payload: user});
const saveUserToDeviceAction = await saveUserToDevice(user);
dispatch(saveUserToDeviceAction);
} catch (error) {
console.log(error);
}
}
export const saveUserToDevice = async(user) => {
try {
//saveUserToDeviceStatus returns true if successful, otherwise returns an object
const saveUserToDeviceStatus = await Keychain.setGenericPassword('user', JSON.stringify(user));
saveUserToDeviceStatus = saveUserToDeviceStatus? true: false;
return {type: SAVE_USER_TO_DEVICE, payload: saveUserToDeviceStatus};
} catch (error) {
console.log(error);
}
}
Метод 4: saveUserToDevice возвращает логическое значение, отправка запускается внутри loginUser
export const loginUser = () => async(dispatch) => {
try {
user = await LoginManager.logInWithPermissions(['public_profile', 'email']);
dispatch({type: LOGIN, payload: user});
const saveUserToDeviceAction = await saveUserToDevice(user);
dispatch({type: SAVE_USER_TO_DEVICE, payload: saveUserToDeviceAction});
} catch (error) {
console.log(error);
}
}
export const saveUserToDevice = async(user) => {
try {
//saveUserToDeviceStatus returns true if successful, otherwise returns an object
const saveUserToDeviceStatus = await Keychain.setGenericPassword('user', JSON.stringify(user));
saveUserToDeviceStatus = saveUserToDeviceStatus? true: false;
return saveUserToDeviceStatus;
} catch (error) {
console.log(error);
}
}