Removed try/catch from async thunks. Letting promise.rejected cases do their work.

This commit is contained in:
gitdagray 2022-04-18 08:24:23 -05:00
parent 82d81f76a8
commit 47554d03da
6 changed files with 18 additions and 54 deletions

View File

@ -11,21 +11,13 @@ const initialState = {
}
export const fetchPosts = createAsyncThunk('posts/fetchPosts', async () => {
try {
const response = await axios.get(POSTS_URL)
return [...response.data];
} catch (err) {
return err.message;
}
const response = await axios.get(POSTS_URL)
return response.data
})
export const addNewPost = createAsyncThunk('posts/addNewPost', async (initialPost) => {
try {
const response = await axios.post(POSTS_URL, initialPost)
return response.data
} catch (err) {
return err.message;
}
const response = await axios.post(POSTS_URL, initialPost)
return response.data
})
const postsSlice = createSlice({

View File

@ -6,12 +6,8 @@ const USERS_URL = 'https://jsonplaceholder.typicode.com/users';
const initialState = []
export const fetchUsers = createAsyncThunk('users/fetchUsers', async () => {
try {
const response = await axios.get(USERS_URL);
return [...response.data];
} catch (err) {
return err.message;
}
const response = await axios.get(USERS_URL);
return response.data
})
const usersSlice = createSlice({

View File

@ -11,21 +11,13 @@ const initialState = {
}
export const fetchPosts = createAsyncThunk('posts/fetchPosts', async () => {
try {
const response = await axios.get(POSTS_URL)
return [...response.data];
} catch (err) {
return err.message;
}
const response = await axios.get(POSTS_URL)
return response.data
})
export const addNewPost = createAsyncThunk('posts/addNewPost', async (initialPost) => {
try {
const response = await axios.post(POSTS_URL, initialPost)
return response.data
} catch (err) {
return err.message;
}
const response = await axios.post(POSTS_URL, initialPost)
return response.data
})
export const updatePost = createAsyncThunk('posts/updatePost', async (initialPost) => {

View File

@ -6,12 +6,8 @@ const USERS_URL = 'https://jsonplaceholder.typicode.com/users';
const initialState = []
export const fetchUsers = createAsyncThunk('users/fetchUsers', async () => {
try {
const response = await axios.get(USERS_URL);
return [...response.data];
} catch (err) {
return err.message;
}
const response = await axios.get(USERS_URL);
return response.data
})
const usersSlice = createSlice({

View File

@ -11,21 +11,13 @@ const initialState = {
}
export const fetchPosts = createAsyncThunk('posts/fetchPosts', async () => {
try {
const response = await axios.get(POSTS_URL)
return [...response.data];
} catch (err) {
return err.message;
}
const response = await axios.get(POSTS_URL)
return response.data
})
export const addNewPost = createAsyncThunk('posts/addNewPost', async (initialPost) => {
try {
const response = await axios.post(POSTS_URL, initialPost)
return response.data
} catch (err) {
return err.message;
}
const response = await axios.post(POSTS_URL, initialPost)
return response.data
})
const postsSlice = createSlice({

View File

@ -6,12 +6,8 @@ const USERS_URL = 'https://jsonplaceholder.typicode.com/users';
const initialState = []
export const fetchUsers = createAsyncThunk('users/fetchUsers', async () => {
try {
const response = await axios.get(USERS_URL);
return [...response.data];
} catch (err) {
return err.message;
}
const response = await axios.get(USERS_URL);
return response.data
})
const usersSlice = createSlice({